diff --git a/docs/basic-features/environment-variables.md b/docs/basic-features/environment-variables.md index d68a614627ed0..5001c080d40c7 100644 --- a/docs/basic-features/environment-variables.md +++ b/docs/basic-features/environment-variables.md @@ -93,7 +93,8 @@ This loads `process.env.NEXT_PUBLIC_ANALYTICS_ID` into the Node.js environment a // pages/index.js import setupAnalyticsService from '../lib/my-analytics-service' -// NEXT_PUBLIC_ANALYTICS_ID can be used here as it's prefixed by NEXT_PUBLIC_ +// 'NEXT_PUBLIC_ANALYTICS_ID' can be used here as it's prefixed by 'NEXT_PUBLIC_'. +// It will be transformed at build time to `setupAnalyticsService('abcdefghijk')`. setupAnalyticsService(process.env.NEXT_PUBLIC_ANALYTICS_ID) function HomePage() { @@ -103,6 +104,18 @@ function HomePage() { export default HomePage ``` +Note that dynamic lookups will _not_ be inlined, such as: + +```js +// This will NOT be inlined, because it uses a variable +const varName = 'NEXT_PUBLIC_ANALYTICS_ID' +setupAnalyticsService(process.env[varName]) + +// This will NOT be inlined, because it uses a variable +const env = process.env +setupAnalyticsService(env.NEXT_PUBLIC_ANALYTICS_ID) +``` + ## Default Environment Variables In general only one `.env.local` file is needed. However, sometimes you might want to add some defaults for the `development` (`next dev`) or `production` (`next start`) environment.