Next.js Discord

Discord Forum

Workaround for runtime env

Unanswered
Southeastern blueberry bee posted this in #help-forum
Open in Discord
Southeastern blueberry beeOP
Ok so, i have a container that compiles the application which is then distributed to different servers, and i have to set NEXT_PUBLIC_* variables at runtime.

This does not seem to be possible, as a workaround i was thinking something like this:

# environment.tsx
"use client"
export const ENV = {};
export function Environment({ env }: { env: Record<string, string> }) {
    Object.assign(env, ENV);
    return null;
}


# layout.tsx
export default function Layout() {
    return <html>
    <body>
        <Environment
            env={{
                APP_URL: process.env.APP_URL!,
            }}
        />
    </body>
    </html>;
}

So i can then use ENV.VAR_NAME in my application.

Would this work? are there better options?

7 Replies

@Yi Lon Ma https://www.npmjs.com/package/next-runtime-env
Southeastern blueberry beeOP
It has not been updated in a long time and this would be a bit of a huge change to Implement. It does not officially implement nextjs 15 either so I don't really feel safe using it
this is the only package I know
though the last released version was 10 months ago, their repo was last updated 2 months ago
maybe you could clone and build the package yourself
@Yi Lon Ma maybe you could clone and build the package yourself
Southeastern blueberry beeOP
I might make my own implementation, or use it for now and hope that in the future someone patches it
Or at worst fork it in the future