Next.js & App Router (50 Qs)•Part 3: Advanced Internals, Scale & Production Architecture (Senior / Lead)
Managing Environment Variables in Next.js.
senior LevelSystem Design
In Simple Words:
- Server-Only Variables: Bare names (
DATABASE_URL,STRIPE_SECRET_KEY). Accessible only in Server Components, Route Handlers, and Server Actions. Stripped from client bundles. - Client-Exposed Variables: Must be prefixed with
NEXT_PUBLIC_(NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY). Baked directly into client JavaScript at build time. - Security Rule: Never prefix sensitive API keys or database URLs with
NEXT_PUBLIC_!