NEXT_PUBLIC_*: the Next.js mistake that cost me a contact form in production

Symptom: in production, the frontend was callingβ¦ localhost:8080. Obviously, nothing answered. π
The cause: NEXT_PUBLIC_* variables are not read when the container starts. They are frozen into the JavaScript bundle at build time.
In practice:
- β Setting
NEXT_PUBLIC_API_URLin the Kubernetes manifest β too late, the bundle is already compiled with the default value. - β
Passing the variable as a build-arg in the Dockerfile, when
next buildruns.
It makes sense once you think about it: this code runs in the visitor's browser, it cannot read the server's environment. But coming from the PHP world where everything is read at runtime, the reflex is not natural.
Moral: always test your production image locally before deploying, not just npm run dev. π


