Replies: 8 comments 24 replies
-
Is there any reason not to use Environment Variables for this? |
Beta Was this translation helpful? Give feedback.
-
Hey @JVMartin
serverRuntimeConfig: {
mySecret: process.env.MY_SECRET,
}
import getConfig from "next/config";
const { serverRuntimeConfig } = getConfig();
function Page({ secret }) {
return <div>My secret: {secret}</div>;
}
Page.getInitialProps = async (ctx) => {
const secret = serverRuntimeConfig.mySecret;
return { secret: secret };
};
export default Page;
MY_SECRET=foobar
MY_SECRET=new value
|
Beta Was this translation helpful? Give feedback.
-
Has there been any new information on this? We're faced with the exact same issue. Our NextJS product is built and baked into a Docker image. This is then ready to be distributed to our customers to install on their own infrastructure, often inside their network firewall. Our app requires a bunch of stuff to be configured during / after deployment: Graph API connection strings, Azure Active Directory configuration etc. Which means we're facing the same challenges: configuration stored in either Specifically for us, a lot of this configuration code is consumed "outside" of a page - e.g. in a hook or library object, I'm not even sure we can we even apply the |
Beta Was this translation helpful? Give feedback.
-
The root problem is that when you build the content statically, you have to choose what content to build. If you have staging content and production content, you can't "build once, deploy anywhere" with static page generation at build time. You either build with staging content or you build with production content. This is true regardless of how you package your build artifacts. You can use docker or tar files or anything, but static content in the image makes it environment specific. The choice seems to me as either of the following:
The "build once, deploy anywhere" philosophy is at odds with having statically generated pages and content built into the image. There isn't a way to do both, regardless of how you import your configuration. This isn't a question of "doing it wrong" or not following best practices. The decisions made by the Next team are optimized for the specific feature of generating static content at build-time and keeping secret configuration from landing in the browser in the process. If you want "build once, deploy anywhere" as the priority, then you would make different decisions in the platform architecture. There wouldn't be a build-time option, instead opting to build on-demand or at server startup. |
Beta Was this translation helpful? Give feedback.
-
Check this lib for the client-side environments |
Beta Was this translation helpful? Give feedback.
-
The Runtime Configuration feature is now considered “legacy” and do not work with Output File Tracing (they gets inlined during build) or React Server Components (crashes the dev server). I’ve written a note about this topic: Runtime Environment Variables in Next.js What I currently do right now: |
Beta Was this translation helpful? Give feedback.
-
To solve for the problem about multiple environments in building Docker of the application NextJs, I wrote the answer here: |
Beta Was this translation helpful? Give feedback.
-
How can I modify the contents of the |
Beta Was this translation helpful? Give feedback.
-
Evidently, it's not possible to use different configurations in staging + production.
We have a NextJS application packaged up in Docker, so that it's environment agnostic. Therefore, we need to supply different configurations for
staging
andproduction
environments, of course. For instance, ourstaging
Next front-end should hit a different back-endAPI_URL
than ourproduction
Next front-end.Per the documentation, we should be able to do runtime configuration like this in our
next.config.js
:If this were true, we would be all set!
But the documentation is definitely wrong, because per the documentation elsewhere:
So since
process.env.*
is always interpolated at build time into the compiled assets, it appears to not be possible to use different configurations in different environments, totally defeating the purpose of environment variables.This totally violates all principles of "build once, deploy anywhere", "immutable deployments", "test the artifact before promoting to production", etc.
Does Next deliberately violate these principles or is this an accident? Any ideas of any hacks or workarounds to let me run a Next application in multiple environments - without multiple build artifacts, because it is obviously critical that the artifact being promoted into production be identical to the one in test / staging environments?
Relevant issues, all totally unresolved:
#1488
#15131
https://stackoverflow.com/questions/60364413/react-next-js-recommended-way-to-set-constants-such-as-backend-api-urls
https://stackoverflow.com/questions/60871862/nextjs-prevent-env-vars-to-be-required-on-build-time
#17641
Beta Was this translation helpful? Give feedback.
All reactions