Next.js Developer interview question · Most asked
How do environment variables work in Next.js, and what is exposed to the browser?
Summary
Variables in .env* files are available on the server via process.env. Only variables prefixed with NEXT_PUBLIC_ are inlined into the client bundle at build time, so never put secrets in NEXT_PUBLIC_ variables.
Longer Answer
NEXT_PUBLIC_ values are baked in at build time, so changing them requires a rebuild (unless you read them at runtime on the server and pass them down). Use the server-only package to make modules containing secrets fail the build if imported into client code.
Key Points
process.envon the serverNEXT_PUBLIC_*inlined in client bundle at build- Never expose secrets with that prefix
server-onlyguards secret modules
More most-asked Next.js Developer questions
- How does data fetching work in the App Router?
- How did caching defaults change in Next.js 15?
- What happens, step by step, when a request reaches a Next.js App Router application?
- What is Next.js and what problems does it solve on top of React?
- What are the differences between the App Router and the Pages Router?
- What is the difference between Server Components and Client Components?
- Explain the App Router file conventions: page, layout, loading, error, not-found, template and route.
- Explain SSR, SSG, ISR and CSR in Next.js.