NEXT_PUBLIC_ prefix
The naming rule in Next.js where only environment variables starting with NEXT_PUBLIC_ are copied into browser code.
Putting a secret in a NEXT_PUBLIC_ variable publishes it to every visitor’s browser.
In more detail
At build time Next.js replaces NEXT_PUBLIC_ variables with their values inside the client bundle, so everyone can read them. Everything else stays on the server, which is where database passwords and API keys belong.
Example
NEXT_PUBLIC_API_URL=https://api.acme.comRelated terms
- Next.js — A React framework for building web applications that can render pages on the server and supports React Server Components.
- environment variable — A named setting such as PORT=8000 that a program reads from its surroundings when it starts.
- secret — Any value that grants access, such as a password, access token or private key, and must be kept out of code and version control.
- bundler — A build tool, such as webpack, Rollup or Vite, that follows imports and combines modules into files a browser can load efficiently.
Taught in
- Environment Configuration: Secrets & Multi-Stage — Production Full-Stack Engineering