Public vs. secret variables
Every environment variable in Next.js starts out server-only. NEXT_PUBLIC_ is not a security feature — it's an opt-in flag that tells the build step 'inline this into the browser bundle.' Nothing else about your setup changes that default.
What this Server Component actually sees
This whole page is a Server Component. It runs only on the server (or, for static content like this, once at build time). It can read every variable in .env.local — public or not.
NEXT_PUBLIC_SITE_NAMEPublicPrefixed with NEXT_PUBLIC_, so Next.js inlines the literal string into the client JS bundle at build time. Safe to show in full.
NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEYPublicStripe's own naming convention: 'publishable' keys are designed to sit in client-side code. More on this in Problem 08.
DATABASE_URLServer-only · maskedNo prefix, so this never gets inlined into client JS. We're only showing a masked preview here on purpose — see the danger box below for what happens if a page prints it in full.
STRIPE_SECRET_KEYServer-only · maskedSame story. Stripe calls this a 'secret' key for a reason: it can charge cards and issue refunds.
The .env.local behind this page
# public — inlined into the browser bundle at build time
NEXT_PUBLIC_SITE_NAME="env.inspector"
NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY="pk_test_DEMO_51FAKEKEYnotreal0000000000"
# server-only — never inlined, only available in Node.js code
DATABASE_URL="postgres://demo_user:demo_password@localhost:5432/demo_db"
STRIPE_SECRET_KEY="sk_test_DEMO_51FAKEKEYnotreal0000000000"{process.env.DATABASE_URL} directly instead of a masked preview, the full connection string — username, password, host — would be sitting in the page's HTML for anyone who views source. "It's a server-only variable" only protects the value while it stays out of anything the server sends back. Problem 06 stages exactly this mistake so you can see it happen.- 1View source on this page (
Ctrl/Cmd+U) and search forenv.inspector. You'll find it in the rendered HTML. - 2Open DevTools → Sources (Chrome) or Debugger (Firefox), then use "Search across all files" for
NEXT_PUBLIC_SITE_NAME. You'll find it hard-coded inside a compiled.jschunk — proof it was inlined at build time, not fetched at request time. - 3Now search all files for a fragment of the database URL, like
demo_password. It won't be there. The masked card above only ever sent a partial preview to the browser.
Any variable you put in .env.local is automatically hidden from the browser.
NEXT_PUBLIC_ variables are replaced with a literal value at build time, not looked up at request time.
A variable without the NEXT_PUBLIC_ prefix is completely inaccessible to Server Components.