Skip to main content

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

Next.js
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_URL in 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 build runs.

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. πŸ˜…

See the original post on LinkedIn