Your .envfile doesn't keep secrets. Where the code runs does.
Ten small, deliberately broken (and fixed) Next.js routes that show exactly how environment variables move between your server, your build, and the browser — and how to check, yourself, what actually ended up in front of a visitor.
NEXT_PUBLIC_SITE_NAMEPublicRendered by a Server Component, but safe: it was always meant to reach the browser. Inspect this page's source — you'll find it in the HTML and in the JS bundle.
DATABASE_URLServer-only · maskedThis Server Component CAN read the full value — but we're deliberately only printing a masked preview. Problem 01 shows what happens the moment you print the real thing into HTML.
Legend
Intentionally shipped to every visitor's browser.
Background you need before the danger cases make sense.
Works, but has sharp edges (limits, freshness, scope).
A pattern that leaks or can easily leak a secret.
The ten problems
each has notes, a live demo, and a self-checkPublic vs. secret variables
The baseline: what NEXT_PUBLIC_ actually does, and what it doesn’t.
The Client Component prop leak
A secret can’t be read in a Client Component — unless you hand it one.
Route Handlers (API routes)
Server-only code, but the JSON you return is still public.
Middleware & the Edge runtime
Middleware runs before your app — and has its own rules.
Server Actions
Functions that look local but run on the server — mind the return value.
"SSR is not an API"
Rendering on the server still means shipping HTML to the browser.
Build time vs. runtime
Why NEXT_PUBLIC_ values get frozen — and how to avoid it.
Publishable vs. secret API keys
Not every key is a secret. Knowing which is which matters.
Vercel environments
Production, Preview, Development — and who can see what.
Inspect it yourself
A field guide to finding leaks with nothing but DevTools and curl.
.env.local.example). The project also ships a real automated check — npm run verify — that builds the app and greps the client bundle for a marker string, so you can confirm nothing server-only made it into .next/static before you ever push to Vercel.