Environment configuration
What you need to know
Environment configuration has two different lifetimes in Next.js: values captured during the build and values read during a server request. Separate public client settings from confidential server configuration before promoting an artifact.
By the end, you can: distinguish public build values from secrets; plan promotion.
Build-time public label in a Client Component
'use client';export default function Banner(){return <p>{process.env.NEXT_PUBLIC_CATALOG_LABEL ?? 'Catalog'}</p>;}Expected behavior: The direct public variable reference can be inlined by a reviewed production build. It is not secret.
How it works
Direct references to NEXT_PUBLIC_ variables in browser code can be replaced during the build; the resulting bytes keep that public value when moved to another environment. Do not expect changing a container variable to rewrite a browser bundle. Values placed in the Next.js config env option are bundled even without the public prefix, so never put credentials there. Server-only runtime settings should be read in a request-time context, not evaluated during a static build. connection() can opt into request-time rendering in the appropriate lane. Validate required configuration without printing values, and use an approved secret store/injection mechanism. Separate preview and production data, secrets, domains and access policies. If public configuration must differ without a rebuild, design a small explicitly public runtime endpoint and validate cache policy. Do not expose a server environment dump.
Read an explicitly public runtime label on the server
import {connection} from 'next/server';export default async function Page(){await connection(); const region=process.env.DISPLAY_REGION; return <p>Region: {region ?? 'unknown'}</p>;}Expected behavior: Only DISPLAY_REGION is intentionally displayed; do not reuse this pattern for credentials. This staged dynamic page reads at request time.
Common mistake
A secret without NEXT_PUBLIC_ can still leak if returned in props, HTML, JSON, config env or logs. Changing a runtime variable does not alter an already compiled public label.
Practice and verification
Classify a public brand label, DB credential, deployment ID and display-region value by confidentiality and lifecycle. Plan promotion of one artifact to preview and production; explain which differences need a new build.
Scope: Proposed Next.js 16.x / React 19, App Router with Cache Components disabled unless noted. Framework and browser examples are staged, not executed. Hypothetical costs are not actual provider rates.