---
title: "NEXT_PUBLIC variables reach the browser: keep secrets out"
description: "Next.js writes the value of any environment variable that starts with NEXT_PUBLIC into the JavaScript every visitor downloads, at next build time. A secret key stays in a variable without that prefix, read on the server only."
url: https://ihsanalapsi.dev/articles/nextjs-public-env-secrets
lang: en
date: 2026-11-25
updated: 2026-11-25
tags: ["App security", "Web development"]
author: Ihsan Alapsi (إحسان العبسي)
---
# NEXT_PUBLIC variables reach the browser: keep secrets out

A small prefix at the start of a variable name decides whether the key to your paid service stays on the server, or ships in a JavaScript file to everyone who opens your site. By the end of this article you will know what the `NEXT_PUBLIC_` prefix does, how to check your project today, and where the secret key goes instead.

## What does the NEXT\_PUBLIC prefix do?

The `NEXT_PUBLIC_` prefix tells Next.js to write the variable's value into the JavaScript files sent to the browser, at `next build` time. A variable without it stays on the server.

According to the [Next.js documentation on environment variables](https://nextjs.org/docs/app/guides/environment-variables), variables that do not start with `NEXT_PUBLIC_` are only available in the Node.js environment, and the browser cannot reach them. With the prefix, Next.js replaces every `process.env.NEXT_PUBLIC_…` reference with the value itself, hard-coded, and it ends up in any JavaScript sent to the browser.

The documentation's own example:

```js
// .env
// NEXT_PUBLIC_ANALYTICS_ID=abcdefghijk

setupAnalyticsService(process.env.NEXT_PUBLIC_ANALYTICS_ID)
// at build time it becomes: setupAnalyticsService('abcdefghijk')
```

## Why is this dangerous for a secret key?

Because the JavaScript files sent to the browser are public: any visitor can open them in the developer tools and read what is inside. A key that goes into them is public from the moment of the build.

The mistake usually goes like this: a component that runs in the browser needs to call a paid service, the unprefixed variable does not reach it, and the code cannot find the value. The developer adds `NEXT_PUBLIC_` to the variable name, and the code works. The problem looks solved, and the key now sits in a file every visitor downloads.

One detail makes the mistake harder to undo: the value is saved at build time. The documentation says that after the build, the app no longer responds to changes to these variables, and they all stay "frozen" with the value from `next build`. So removing the key from your `.env` file alone does not remove it from the deployed version.

## How do you find a secret that reaches the browser?

You find it with two checks: the name of every public variable in your project, and the files of the live site itself.

1. Search your project for `NEXT_PUBLIC_` and read every name. An analytics ID or a public URL is fine. Any name with `SECRET` or `PRIVATE` in it, or the key to a service you pay for, needs a closer look.
2. Open your live site, and in the browser's developer tools search the JavaScript files for the first few characters of the key. If you find them, the key is public.
3. If a secret key got out: replace it in the service's own dashboard first, then remove the prefix and build the site again. The old key has been seen, and a new build does not make it secret again.

The same logic applies to the `.env` files themselves: the documentation says the `create-next-app` template adds all `.env` files to `.gitignore`, and that you almost never want to commit them. If you worry about a key pushed by mistake, [GitHub push protection](/articles/github-push-protection-secrets) stops it before it lands.

## Where does the secret key go instead?

The secret key stays in a variable without the prefix, read only by code that runs on the server, such as a Route Handler. The browser asks the server for the result and never sees the key.

The documentation gives this exact shape, with the database variables staying in the Node.js environment:

```js
// app/api/route.js
export async function GET() {
  const db = await myDB.connect({
    host: process.env.DB_HOST,
    username: process.env.DB_USER,
    password: process.env.DB_PASS,
  })
  // ...
}
```

The same idea works for any paid service: write a Route that calls the service with the key and sends only the result back to the browser. While you write it, make sure the key does not end up [in your server logs](/articles/logs-sensitive-data).

## Where do you start now?

The rule is simple: `NEXT_PUBLIC_` means "public to every visitor", so a secret stays without it and is read on the server. Open your project's `.env` file now and read every name that starts with this prefix. And if you want someone to check your whole platform before someone else finds what leaks, [Muhassan](/muhassan) does that. What was the first variable you found?

## FAQ

**If I change a NEXT_PUBLIC value on the server after deploying, does the site change?**

No. The value is written into the files at next build time and stays the same until you build the app again.

**Can an analytics ID be a NEXT_PUBLIC variable?**

Yes. An analytics ID is meant to reach the browser anyway, and the Next.js documentation itself uses NEXT_PUBLIC_ANALYTICS_ID as its example.

**Should I commit .env files to the repository?**

No. The create-next-app template adds all .env files to .gitignore, and the documentation says you almost never want to commit them.
