Secrets
NEXT_PUBLIC_ inlines a secret at build time
Cataloged October 16, 2026 · Cursor · v0
Symptom
A key was added as NEXT_PUBLIC_ so a client component could read it. The production bundle contains the value.
Root cause
Next.js documents that only variables prefixed with NEXT_PUBLIC_ are inlined into the browser bundle, at build time, replacing process.env.NEXT_PUBLIC_... with the literal value. Other variables stay on the server. v0's integration notes say its Vars panel stores a key for server code, and that the key must not be used in a client component. The same notes say the Vars panel writes to all Vercel environments by default, so a live key can land on preview deployments unless you scope it in the Vercel dashboard.
Before and after
Next.js documents the prefix. The after read belongs in a Route Handler or Server Action, not in a client component.
Before
process.env.NEXT_PUBLIC_SECRET
After
process.env.SECRET_NAME
Fix prompt
In this Next.js project only: 1. List every NEXT_PUBLIC_ variable. Remove the prefix from anything that is a secret. 2. Move the call that needs the secret into a Route Handler or Server Action. Read process.env.SECRET_NAME only there. 3. If this project is on v0, set production and preview values separately in the Vercel project's environment settings instead of one shared Vars entry. 4. Redeploy. NEXT_PUBLIC_ values are frozen at build time, so a dashboard edit does not update an existing deployment until you rebuild. Do not print secret values.
Paste this into the coding tool that has your own project open.
2-minute self-check
These steps are for an app you own. Do not run them against someone else's project.
- Search your own repo for NEXT_PUBLIC_. Each one should be a value you are willing to show a visitor.
- Open your own deployed site, view source, and search for a secret you know. It should not appear.
- In the Vercel project for your own app, confirm the live secret is not attached to Preview.
Common questions
I added NEXT_PUBLIC_ so a client component could read the key. Where did the value go?
Next.js inlines only NEXT_PUBLIC_ variables into the browser bundle, at build time, replacing process.env.NEXT_PUBLIC_... with the literal value. Other variables stay on the server. A dashboard edit does not update an existing deployment until you rebuild. View source on your own deployed site and search for a secret you know.
The project is on v0. Does the Vars panel keep the key on the server?
v0's integration notes say the Vars panel stores a key for server code, and that the key must not be used in a client component. The same notes say the panel writes to all Vercel environments by default, so a live key can land on preview deployments unless you scope it in the Vercel dashboard.
Longer guide
- OpenAI API key exposed in the frontend: check your own app
Which keys are meant to be public, why a NEXT_PUBLIC_ or VITE_ prefix ships the others, and what to do on your own app if a secret key is in the bundle or the git history.