Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify conditions under which process.env expressions are inlined #33212

Merged
merged 3 commits into from
May 22, 2022

Conversation

jameshfisher
Copy link
Contributor

@jameshfisher jameshfisher commented Jan 12, 2022

Documentation / Examples

  • Make sure the linting passes by running yarn lint

@@ -88,13 +88,14 @@ In order to expose a variable to the browser you have to prefix the variable wit
NEXT_PUBLIC_ANALYTICS_ID=abcdefghijk
```

This loads `process.env.NEXT_PUBLIC_ANALYTICS_ID` into the Node.js environment automatically, allowing you to use it anywhere in your code. The value will be inlined into JavaScript sent to the browser because of the `NEXT_PUBLIC_` prefix. This inlining occurs at build time, so your various `NEXT_PUBLIC_` envs need to be set when the project is built.
This loads `process.env.NEXT_PUBLIC_ANALYTICS_ID` into the Node.js environment automatically, allowing you to use it anywhere in your code. To expose these variables to JavaScript on the browser, any JavaScript expressions of the form `process.env.NEXT_PUBLIC_FOO` will be inlined at build time, replaced with the string value of the `NEXT_PUBLIC_FOO` environment variable set when the project is built. For example:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this reads any better. In fact, it assumes the reader understands what FOO means. Let's revert this paragraph and keep the comment on line 98.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Linguistics are easy to bike shed on - I think the original is maybe slightly better linguistically. But I think the key thing that @jameshfisher is trying to emphasize here is that the build process will look for process.env.NEXT_PUBLIC_FOO. We could also write process.env.NEXT_PUBLIC_* or something like that.

@devinrhode2
Copy link

devinrhode2 commented Jan 18, 2022

The current docs gave me the impression that I HAVE to write process.env.FOO or process.env.NEXT_PUBLIC_FOO.
But.. I'm able to basically do process.env[envVarParam] and everything still works.

I think the inlining is important for process.env.NODE_ENV === 'development' - I'm not certain but I'd assume something in the build process will strip out if ('production' === 'development') {/* development-only logs */}

@devinrhode2
Copy link

@styfle @jameshfisher would this be a good place for the docs to mention how development-only checks can be stripped out of production builds?

@styfle
Copy link
Member

styfle commented Jan 18, 2022

But.. I'm able to basically do process.env[envVarParam] and everything still works.

Thats odd because I didn't think it would work and also your PR here says dynamic usage does not work.

I just realized you're not the author of this PR.

The dynamic usage might work locally or for Serverless Functions, but it doesn't work in browser bundles or in Edge Functions (like Middleware).

@styfle
Copy link
Member

styfle commented Jan 18, 2022

I think the inlining is important for process.env.NODE_ENV === 'development'

Agreed, this is important. I can't seem to find any docs on this behavior so we can document it here.

@devinrhode2
Copy link

devinrhode2 commented Jan 23, 2022

The dynamic usage might work locally or for Serverless Functions, but it doesn't work in browser bundles or in Edge Functions (like Middleware).

Yep, found that one out the hard way :(
Turns out it doesn't work locally on windows (10 or 11, also tried with cygwin on windows 11) :/

@ijjk ijjk requested a review from balazsorban44 as a code owner May 22, 2022 22:48
ijjk and others added 2 commits May 22, 2022 17:48
@kodiakhq kodiakhq bot merged commit 82fb4b4 into vercel:canary May 22, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 22, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants