Skip to content
← All posts

Dec 2, 2026

Your secret is in the client bundle — NEXT_PUBLIC_ is not a vault

Every Next.js dashboard needs a public value in the browser — an API URL, an analytics ID. NEXT_PUBLIC_ is how you put it there. But the prefix is a promise that the value is public, and Next.js keeps that promise by writing it into the JavaScript bundle as a literal. Put a real secret behind that prefix and every visitor can read it.

Next.jsTypeScriptSecurity

Every Next.js dashboard needs something public in the browser. The API base URL, an analytics ID, the site URL. The way you get a value into client code is the NEXT_PUBLIC_ prefix:

# .env.local
NEXT_PUBLIC_API_URL=https://api.example.com

It works, and it works so easily that it hides what it actually does. The prefix is not a flag that says "this variable is allowed in the browser". It is a promise that the value is public — and Next.js keeps that promise by writing the value into the JavaScript bundle as a literal string, shipped to every visitor who loads the page.

Inlining happens at build time, and it is literal

When you run next build, Next.js does a find-and-replace over your code. Every static reference to process.env.NEXT_PUBLIC_API_URL becomes the string the variable held on the build machine at that moment:

// What you wrote
const url = process.env.NEXT_PUBLIC_API_URL;

// What the browser receives
const url = "https://api.example.com";

There is no lookup at runtime. There is no process.env object in the browser at all. The deployed bundle contains the literal value, not a reference to it — which is also why changing the variable in your hosting dashboard does nothing until the next build. The running bundle is frozen at the last build.

The replacement is textual, which produces one failure mode that looks like a Next.js bug and is not:

// Works in the browser — a static reference the bundler can find
const url = process.env.NEXT_PUBLIC_API_URL;

// undefined in the browser — no process.env object exists to index into
const name = "NEXT_PUBLIC_API_URL";
const url2 = process.env[name];

// Also undefined — destructuring is not a static reference either
const { NEXT_PUBLIC_API_URL } = process.env;

Server code has none of these restrictions. Inside a Server Component, a Route Handler, or a Server Action, process.env is the live Node.js object, read at request time, dynamic keys and all. The rule is simple: the server reads variables, the browser only ever sees literals that were baked in at build time.

The leak is usually one prefix away

The dangerous case is not a variable you meant to be public. It is a real secret that picked up the prefix by accident — copied from a tutorial, renamed during a refactor, or added in a hurry because the code that needed it ran in a Client Component:

# .env.local — do not do this
NEXT_PUBLIC_STRIPE_SECRET_KEY=sk_live_...
NEXT_PUBLIC_SUPABASE_SERVICE_ROLE_KEY=eyJ...

A service-role key bypasses every row-level security policy in the database. A Stripe secret key can create charges and refunds. Both are now sitting in a JavaScript file that anyone can download, search, and read in DevTools. There is no mechanism to make a NEXT_PUBLIC_ variable secret after the build — the value is embedded in static JavaScript, and the only remediation is renaming the variable, rebuilding, and rotating the secret.

The prefix is not the only way a secret escapes. A Server Component that passes a secret down as a prop to a Client Component serialises it into the RSC payload, embedded in the HTML inside self.__next_f.push(...) script tags. And a Client Component that imports a module which reads the secret pulls the whole module into the browser bundle. Neither looks like a leak in the code — both are.

The fix: keep secrets on the server, and make accidents fail the build

First, drop the prefix and read the secret only in server code — a Route Handler, a Server Action, or a Server Component that never hands the value to a client boundary:

// app/api/charge/route.ts — server only, never shipped to the browser
import Stripe from "stripe";

const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);

export async function POST(req: Request) {
  // the secret key never leaves this function
}

Second, make the accident impossible instead of unlikely. The server-only package turns "a Client Component imported my secrets module" into a build error instead of a silent leak:

// lib/db.ts
import "server-only";

export const db = /* ... uses process.env.DATABASE_URL ... */;

Import that module from a Client Component and the build fails, loudly, before anything ships. The same idea applies to environment variables themselves: validate them with a Zod schema at startup, split into server and client, so a missing variable fails next build instead of the first production request — and so a server variable can never be read from client code without an error.

Third, treat anything that already shipped as compromised. Deleting the line from the source does not remove it from git history, and it does not remove it from the bundles already deployed. Rotate the secret, then fix the code. The default create-next-app template puts .env files in .gitignore for exactly this reason — almost never commit them — but a gitignored file that was once committed is still in the history until the secret is rotated.

The honest limits

NEXT_PUBLIC_ is not the enemy. Publishable keys, analytics IDs, and the site URL belong there — they are public by design, and the prefix is the correct way to expose them. The mistake is treating the prefix as a way to reach a value from the browser, rather than as a declaration that the value is safe for anyone to read.

A bare unprefixed process.env.SECRET read inside client code is replaced with an empty string, not leaked — but do not rely on that as a safety net. It is an implementation detail, and the prop and import routes above bypass it entirely. And none of this replaces the basics: a leaked secret is rotated, not deleted; a .env file stays out of the repository; and the question to ask before any variable gets the prefix is the only one that matters — would I be comfortable if this value were printed on the homepage?

If the answer is no, it does not get the prefix. It stays on the server, where the browser cannot see it.