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
"Runtime" configuration variables are baked in at compile time when using standalone as target #36551
Comments
An explanation to this has been given here: #33910 (comment) You could also simply use |
Thank you for the reply, the thread that you referenced is very relevant indeed. There's some confusion still on my side which may simply be an issue with the documentation. According to https://nextjs.org/docs/basic-features/environment-variables
I've taken this to mean that all process.env variables are baked in at build time. Additionally, while the process.env at runtime works for the server side configuration, it does not allow for runtime modification of client side config parameters, which was the original reason for my using this pattern. This model is also suggested in the documentation on runtime configuration: https://nextjs.org/docs/api-reference/next.config.js/runtime-configuration |
I agree, the docs could be more clear about that. 👍 I think it was overlooked, only client-side values are actually replaced: #20869 (comment)
Runtime configuration on client-side comes with the penalty of not being able to statically optimize the page (described here), so it would be good to know your use case. Are you trying to serve content based on the user request/other dynamic parameters? |
Thank you for the reply. My use case is to be able to build a docker image once, then run it across our different environments (development, staging, production). For the clientside, the bits that differ are only some URLs such as the domain used for API calls, some environment specific details passed to Sentry for error reporting, to Google Analytics, etc. I appreciate that the most performant way of doing this would be to build an image per environment, though I'd like to avoid it if possible as no other parts of the system works in this way, e.g. backend services are all built once, then simply fired up in an environment and tweaked via environment variables. When I've done this with "traditional" react projects, I've injected a single dynamic config file loaded once on startup of the SPA. I suppose it would be possible to do this also with Next and wrap each client page in a component that reads (and if needed caches) the config with a regular REST call, and have a single dynamic API endpoint depend on environment variables on the server. |
This request sounds like what @leerob concluded here as well: #35820 (comment) |
Agreed, this looks like the same type of use case. |
Just ran into this as well! Thank you for making this clear 👍 @balazsorban44 |
Based on #36551 (comment) making the docs more clear which also aligns with the wording of Environment Variables in Middlerware: https://nextjs.org/docs/api-reference/next/server#how-do-i-access-environment-variables See also: #20869 (comment) ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `yarn lint` Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Verify canary release
Provide environment information
What browser are you using? (if relevant)
N/A
How are you deploying your application? (if relevant)
N/A
Describe the Bug
I'm leveraging the standalone target (outputStandalone: true) in order to create a self contained build suitable for use in Docker. While I do get an output application, this does not appear to play nice with runtime configuration.
I understand that normally in Next, access to process.env is resolved at build time, but it is possible to inject runtime configuration by accessing process.env (or through other means) in next.config.js.
When using the standalone build however, the config form next.config.js appears to be baked into the generated server.js file, meaning any environment variables set at runtime (such as through the Docker configuration) are ignored.
Expected Behavior
Config is read by evaluating next.config.js (or the original content of it) at runtime when initializing the next standalone server.
To Reproduce
The text was updated successfully, but these errors were encountered: