Next.js Discord

Discord Forum

.env variables change at runtime in production

Unanswered
darkej posted this in #help-forum
Open in Discord
I have a problem that I haven't been able to figure out for a few days now. I have an application that consists of many separate microservices using docker and docker-compose. The application resides on a client server that is closed (no access to world wide). I am currently building another microservice - frontend using Next13js app router. The purpose of this service is to display a large number of records retrieved from another service via API. To improve performance I want to use fetch revalidate because the data only changes every few hours. The problem is that during the build next tries to fetch the data but this data is inaccessible due to being in a closed environment. Also, I would not want the client data to be in the image. So I thought of trying to use an environment variable and during the build set it to true to fetch the mock and during the runtime set it to false for next to fetch the real data already. This solution doesn't work, because the environment variables from .env are loaded fully into the bundle and later at runtime they can't be changed - next doesn't respond to the environment variables at all in npm run start or node server.js mode - no matter if I specify them from a file or set them manually or if it's given by docker or docker-compose. The flow that I want to achive is:

1. Build a my Next13js application docker image
2. Fetch mock during build
3. Upload image to the production server
4. Run application
5. Fetch valid data

Does anyone have any idea how I could solve my problem ?
Or maybe how I can change the flow or logic ?

13 Replies

Giant panda
Curious of the above too, however if you're using next13 and app dir (more specifically server components, the above solution doesn't work right?), using next server/public runtime config disables RSC
about runtime configs
It looks like I just solve the issue setting
export const dynamic = 'force-dynamic'
Giant panda
So you're able to use RSC + the legacy runtimeConfig? interesting!
cool. Next.js, developed by Vercel, which is designed to run on serverless env like AWS lambda and Cloudflare worker, so they are not serious abt k8s, containers envirionment on AWS, GCP and so on.
@Giant panda So you're able to use RSC + the legacy runtimeConfig? interesting!
No, I am not using runtime config. I am using .env + env setting by docker-compose
Giant panda
Ah so, you're saying you are just applying export const dynamic = 'force-dynamic' to all of your pages to make it pull in environment variables at runtime?
@Giant panda Ah so, you're saying you are just applying `export const dynamic = 'force-dynamic'` to all of your pages to make it pull in environment variables at runtime?
not for all pages but chosen one and yes with this option next is reading env variables dynamicly but using this solution we are getting rid of caching. I will still try to investigate this problem and try to find best solusion. Now I need to do some tests and see how it's behave on production
I will share further information here