Skip to content

Instantly share code, notes, and snippets.

@HugoDF
Created July 2, 2020 09:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HugoDF/2ecdce9d589aee56f2991b76c61649cc to your computer and use it in GitHub Desktop.
Save HugoDF/2ecdce9d589aee56f2991b76c61649cc to your computer and use it in GitHub Desktop.
Next.js notes

When using a custom Next.js server there are many ways for environment variables to make it into the application:

  • server-side outside of Next (using dotenv)
  • server-side with Next (at build time)
  • server-side with Next (at run time)
  • client-side with Next (at build time)
  • client-side with Next (at run time)

For build time variables:

  • server-side: access using process.env (dotenv or Next will likely pick it up)
  • client-side: map from process.env to env in next.config.js

For runtime variables (see https://nextjs.org/docs/api-reference/next.config.js/runtime-configuration):

  • server-side: map from process.env to serverRuntimeConfig or publicRuntimeConfig in next.config.js
  • client-side: map from process.env to publicRuntimeConfig in next.config.js

Other note:

  • Next.js 9.4 will automatically make NEXT_PUBLIC_* env variables available client-side
  • Next.js 9.4? loads .env files variables into process.env
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment