Make environment variables customizable! #51597
GoalsFlexibility BackgroundReason: For an application that has multiple environment modes, it is not possible to load environments for all of them. For example: I have an app with two modes: production and development. Both modes must be deployed with environments based on their respective modes. Whenever I build a Next.js app, it loads the environment from the .env.production file. Proposalit would be cool if we could load envs like this: "scripts": { |
Replies: 1 comment
|
This should be solved with the App Router. We can safely read environment variables on the server during dynamic rendering. This allows you to use a singular Docker image that can be promoted through multiple environments with different values. So you could use whatever environment you want. Build once, run anywhere. import { unstable_noStore as noStore } from 'next/cache';
export default function Component() {
noStore(); // Opt-into dynamic rendering
// This value will be evaluated at runtime
const value = process.env.MY_VALUE
...
}You can also opt into dynamic rendering in other ways, like using |
This should be solved with the App Router. We can safely read environment variables on the server during dynamic rendering. This allows you to use a singular Docker image that can be promoted through multiple environments with different values. So you could use whatever environment you want. Build once, run anywhere.
You can also opt into dynamic rendering in other ways, like using
cookiesorheaders, but this is an example.