Skip to content
tezvyn:

How do you manage Next.js env variables on Vercel and use NEXT_PUBLIC_?

Source: nextjs.orgEasyHow cards are made

Summary

Server-client boundary awareness and secret hygiene.

Key points

Set DB strings in Vercel dashboard without NEXT_PUBLIC_; consume in server code only. NEXT_PUBLIC_ inlines values into the browser JS bundle, leaking them to all users.

What's really being asked

This question tests whether you understand the boundary between server and client code in Next.js and whether you treat environment variables as a security surface. Interviewers want to see that you know where secrets live, how they are bundled, and why a database connection string must never reach the browser. They also care if you understand the build-time versus runtime distinction for client-side code.

The full answer

First, explain that sensitive variables like a database connection string should be added in the Vercel project dashboard under Environment Variables or in a local .env file, and they must not use the NEXT_PUBLIC_ prefix. Second, clarify that non-prefixed variables are only accessible in Node.js contexts such as Server Components, API routes, route handlers, getServerSideProps, or middleware. Third, state that NEXT_PUBLIC_ exists specifically to expose values to the browser, because Next.js replaces process.env.NEXT_PUBLIC_X with the literal string at build time so the client bundle can use it without a server round-trip. Fourth, describe the risk: any value prefixed with NEXT_PUBLIC_ becomes part of the static or server-rendered JavaScript sent to every user, meaning secrets are trivially recoverable from the page source or network tab.

The mistakes people make

A major red flag is saying you would prefix a database URL with NEXT_PUBLIC_ so the frontend can fetch data directly. Another is claiming that Vercel encrypts NEXT_PUBLIC_ variables in the browser bundle or that the prefix is just a naming convention with no security impact. Some candidates also confuse server-side rendering with server-side secrecy, arguing that because the variable is rendered on the server first, it stays hidden; this misses that the inlined literal still ships to the client.

What usually comes next

The interviewer may ask how to rotate a leaked secret, what to do if you accidentally committed an env file to git, or how to share a variable between server and client safely. They might also probe whether you know that NEXT_PUBLIC_ variables are set at build time on Vercel, so changing them requires a rebuild unless you use runtime configuration strategies.

A concrete example

Imagine you need a public Stripe publishable key for checkout. You set NEXT_PUBLIC_STRIPE_KEY in the Vercel dashboard and reference it in a client component to initialize Stripe.js. This is correct because the key is designed to be public. In contrast, your Postgres connection string belongs in DATABASE_URL with no prefix, and you should call it only inside a Server Component or an API route that queries the database and returns JSON to the client.

Interview question

A teammate prefixes the Postgres DATABASE_URL with NEXT_PUBLIC_ so a client component can query it directly. What is the most important problem with this approach?

  • a.The value stays hidden because it is rendered on the server and never sent to the client
  • b.The database credentials become embedded in the client JavaScript bundle visible to every userCorrect
  • c.The prefix is only a naming convention and does not change how the variable is bundled
  • d.Vercel encrypts the value in the browser bundle, keeping the credentials safe from users
Why?

NEXT_PUBLIC_ tells Next.js to inline the literal value into the client bundle at build time, so the credentials are visible in the page source to every user. This is not prevented by server-side rendering, because the string is still shipped to the browser for hydration.

Just read this? Test yourself on what you have been reading.

Read the original → nextjs.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on nextjs — each one lists the topics its interview covers.

See open roles