# That NEXT_PUBLIC_ variable ships to the browser

> NEXT_PUBLIC_* values are inlined into the client bundle. Anything private stays on the server.

Source: https://fullbytedeveloper.com/blog/public-env
Published here: 2026-11-20, updated 2026-12-03

Next.js inlines NEXT_PUBLIC names into browser JavaScript at build time, so anyone who downloads the bundle can read those values [1]. Names without that prefix stay on the server. After a build, grep the static output for the prefix and for any secret you never meant to ship.

## Quick start: name the secret without the public prefix

Put client-safe config under a public prefix. Put anything that can open a database, charge a card, or mint a session under a name with no public prefix.

```bash
cat > /tmp/env-demo/.env <<'EOF'
NEXT_PUBLIC_ANALYTICS_ID=G-PUBLICONLY
DATABASE_URL=postgres://app:secret@db.internal:5432/app
STRIPE_SECRET_KEY=sk_test_do_not_ship
EOF
```

In app code, read the public id from `process.env.NEXT_PUBLIC_ANALYTICS_ID` in client components. Read `DATABASE_URL` and `STRIPE_SECRET_KEY` only in Route Handlers, Server Components, or other Node-only paths [1]. The Next.js docs say a regular environment variable is only available in the Node.js environment, and that Next.js can inline a value at build time into the JS bundle delivered to the client when the name uses the `NEXT_PUBLIC_` prefix [1].

## Prove what landed in the client bundle

You do not need a full Next app to understand the rule. The build replaces references to public names with hard-coded strings. After a real `next build`, the static folder under `.next/static` is the place to look.

Simulated client chunk (the same shape a bundler leaves behind):

```bash
mkdir -p /tmp/env-demo/.next/static/chunks
printf '%s\n' 'self.__NEXT_DATA__={};const x="G-PUBLICONLY";/* NEXT_PUBLIC_ANALYTICS_ID inlined */' \
  > /tmp/env-demo/.next/static/chunks/main-app.js
cd /tmp/env-demo
grep -r 'NEXT_PUBLIC_' .next/static
grep -r 'G-PUBLICONLY' .next/static
grep -rE 'sk_test|DATABASE_URL|postgres://' .next/static || echo 'no secrets in client output'
```

Captured output:

```text
.next/static/chunks/main-app.js:self.__NEXT_DATA__={};const x="G-PUBLICONLY";/* NEXT_PUBLIC_ANALYTICS_ID inlined */
.next/static/chunks/main-app.js:self.__NEXT_DATA__={};const x="G-PUBLICONLY";/* NEXT_PUBLIC_ANALYTICS_ID inlined */
no secrets in client output
```

The public analytics id is searchable in the static tree. The database URL and Stripe secret are not. On a real project, run the same greps after `next build`. If a private string appears under `.next/static`, the name that held it was public (or something else copied the value into client code). Move the secret to a server-only name, remove the client reference, rebuild, and grep again [1].

<figure>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 720 280" role="img" aria-label="Build inlines NEXT_PUBLIC values into the client bundle; server-only names stay on Node">
  <defs>
    <marker id="pubEnvArrow" markerWidth="10" markerHeight="10" refX="8" refY="3" orient="auto">
      <path d="M0,0 L8,3 L0,6 Z" fill="#14b8a6"/>
    </marker>
  </defs>
  <rect x="0" y="0" width="720" height="280" rx="12" fill="#0b1f3a"/>
  <text x="24" y="36" fill="#f8fafc" font-family="ui-sans-serif, system-ui, sans-serif" font-size="16" font-weight="700">.env at build time</text>
  <rect x="24" y="56" width="220" height="88" rx="8" fill="#123052" stroke="#14b8a6" stroke-width="2"/>
  <text x="36" y="84" fill="#f59e0b" font-family="ui-monospace, monospace" font-size="14">NEXT_PUBLIC_*</text>
  <text x="36" y="108" fill="#f8fafc" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">safe for browsers</text>
  <rect x="24" y="160" width="220" height="88" rx="8" fill="#123052" stroke="#64748b" stroke-width="2"/>
  <text x="36" y="188" fill="#cbd5e1" font-family="ui-monospace, monospace" font-size="14">DATABASE_URL</text>
  <text x="36" y="212" fill="#cbd5e1" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">server only</text>
  <path d="M244 100 H300" stroke="#14b8a6" stroke-width="3" fill="none" marker-end="url(#pubEnvArrow)"/>
  <path d="M244 204 H300" stroke="#64748b" stroke-width="3" fill="none" marker-end="url(#pubEnvArrow)"/>
  <rect x="308" y="56" width="180" height="88" rx="8" fill="#123052" stroke="#14b8a6" stroke-width="2"/>
  <text x="320" y="84" fill="#f8fafc" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">client JS bundle</text>
  <text x="320" y="108" fill="#f59e0b" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">value hard-coded</text>
  <rect x="308" y="160" width="180" height="88" rx="8" fill="#123052" stroke="#64748b" stroke-width="2"/>
  <text x="320" y="188" fill="#f8fafc" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">Node process.env</text>
  <text x="320" y="212" fill="#cbd5e1" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">not in .next/static</text>
  <path d="M488 100 H544" stroke="#14b8a6" stroke-width="3" fill="none" marker-end="url(#pubEnvArrow)"/>
  <rect x="552" y="56" width="144" height="88" rx="8" fill="#123052" stroke="#f59e0b" stroke-width="2"/>
  <text x="564" y="84" fill="#f8fafc" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">any visitor</text>
  <text x="564" y="108" fill="#f59e0b" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">can read it</text>
</svg>
</figure>

*Public-prefix values become hard-coded strings in the client bundle; server-only names never appear under .next/static.*

## Same trap in Vite and Expo

Vite exposes variables prefixed with `VITE_` on `import.meta.env` after bundling. Its docs warn that `VITE_*` values are bundled into source at build time and should not hold API keys [2]. Expo replaces `process.env.EXPO_PUBLIC_*` references when you run Expo CLI, and says not to store private keys in those names because they show up in plain text in the compiled app [3].

```text
# Vite
VITE_SOME_KEY=123          # client
DB_PASSWORD=foobar         # not exposed

# Expo
EXPO_PUBLIC_API_URL=https://staging.example.com   # client
EXPO_PUBLIC_API_KEY=abc123                        # also client: treat as public
```

If you migrate between frameworks, rename the prefix and re-check the built assets. A secret that was safe as `DATABASE_URL` in Next becomes a leak the moment someone renames it to `NEXT_PUBLIC_DATABASE_URL` "so the client can connect."

## What belongs on the client

Safe client values are ones you would paste into a public README without rotating keys: a marketing site URL, a publishable analytics measurement id, a Stripe publishable key (`pk_...`), a feature-flag client key that only toggles UI. Unsafe client values are database URLs, Stripe secret or restricted keys (`sk_...`, `rk_...`), private API tokens, session signing secrets, and service-account JSON.

Publishable Stripe keys are designed for browsers. Secret and restricted keys are not. Putting `STRIPE_SECRET_KEY` behind `NEXT_PUBLIC_` is the classic mistake this page exists to stop [1].

Changing a public value requires a rebuild because the old string is already baked into the deployed JS [1]. Hosting env panels that only restart the Node process will update server-only names; they will not rewrite an already shipped client chunk until you build again.

## Mistakes that ship secrets

Prefixing a database URL or private API token with `NEXT_PUBLIC_` so a client component can "just read it," then discovering the string under `.next/static` after release.

Reading `process.env.SECRET` in a Client Component and assuming the bundler will keep it server-side. Client modules that touch `process.env` for a public name get inlining; a non-public name often becomes `undefined` in the browser, which tempts people to flip the prefix instead of moving the call to a Route Handler [1].

Committing `.env` because create-next-app put it in `.gitignore` "so it should be fine," then copying the same file into a Docker build context that ignores `.dockerignore` rules. The public values are meant to be public; the rest still need a secrets store.

Treating Expo `EXPO_PUBLIC_API_KEY` as private because the name says key. Expo documents that those values are visible in the compiled application [3].

Skipping the post-build grep after a rename. The UI can look fine while the previous secret still sits in an old chunk on a CDN until cache expiry.

## How this fits agent and webhook work

Agents that edit `.env` will happily add the public prefix when a client error says a variable is undefined. Keep a short allowlist of public names in the repo README, and reject new `NEXT_PUBLIC_` entries that look like secrets in review. For Claude Code projects that sit next to real env files, [lock Claude Code out of your .env](/blog/claude-code-env-lock) so the model cannot paste private values into client files in the first place.

Webhooks and payment confirmations belong on the server with secret keys. Client code should call your own route, not hold the Stripe secret that [six Stripe webhook checks](/blog/webhook-checklist) assume stays private.

## CI check you can paste

Add a job step after the production build that fails if a denylist of secret shapes appears under the static output. Keep it boring:

```bash
# after next build
if grep -RInE 'sk_live_|sk_test_|postgres://|BEGIN (RSA |OPENSSH )?PRIVATE KEY' .next/static; then
  echo 'secret-shaped string found in client bundle' >&2
  exit 1
fi
grep -RIn 'NEXT_PUBLIC_' .next/static | head
```

The first grep is the gate. The second is a review aid so you still see which public names shipped. Expand the denylist for your stack (AWS key prefixes, GitHub PATs, Slack bot tokens). False positives on docs strings are rare in `.next/static`; when one appears, narrow the pattern instead of deleting the check.

Preview deployments deserve the same grep. A "temporary" `NEXT_PUBLIC_STRIPE_SECRET` on a Vercel preview is still a public URL with a downloaded bundle.

## Dynamic lookup does not save you

Expo documents that only static `process.env.EXPO_PUBLIC_*` property access is inlined; bracket access is not [3]. Next's public inlining is also a build-time rewrite of references it can see [1]. People sometimes write `process.env[name]` hoping to hide a secret from the bundler while still reading it on the client. That usually yields `undefined` in the browser, which sends them back to adding the public prefix. Move the call to a Route Handler or server action instead of inventing a cleverer client read.

Feature-flag vendors and analytics SDKs often want a client key. That is fine when the vendor designed the key to be public. Copying the same pattern onto your own admin API token is how incidents start.

## When the grep is clean

After `next build` (or your Vite/Expo production build), the static tree shows only the public ids you intended, and a search for `sk_`, `postgres://`, and private token prefixes returns nothing. Deploy that build. Rotate any secret that ever appeared under a public prefix, because browsers and scrapers may already have it.

Server-only names keep working when you change them in the host's env panel and restart. Public names need a fresh build. That asymmetry is the whole design: the browser bundle is a snapshot, not a live view of `process.env` [1][2][3].

Before any public AI paste of a work PDF, run the file-class check in [check approval before uploading work documents](/blog/sensitive-documents-public-ai-boundaries).

Browse the [Dev tools topic](https://fullbytedeveloper.com/blog/topic/dev-tools).

Send this to the friend who put a private API key behind a NEXT_PUBLIC name.

Image: A server vault retaining private gems in a dark inner chamber, while public tokens are baked as glowing shards into a client bundle plane that flies out into an open browser-side space, with the two regions clearly split and no typography or variable names.
https://fullbytedeveloper.com/media/public-env/public-env-venice.webp
Values marked for the client are inlined into the bundle. Private secrets stay on the server.

## Sources

1. [Next.js docs: environment variables](https://nextjs.org/docs/app/building-your-application/configuring/environment-variables)
2. [Vite docs: env variables and modes](https://vite.dev/guide/env-and-mode.html)
3. [Expo docs: environment variables](https://docs.expo.dev/guides/environment-variables/)
