Performance & Deploy · 9 min read · Intermediate
Environment Variables, Secrets & Security Headers
Separate server-only secrets from NEXT_PUBLIC_ browser variables and configure security headers in next.config.ts.
What you'll learn
- Never prefix database passwords or API secrets with NEXT_PUBLIC_
- Use the server-only package to guard secret helper modules at build time
- Configure X-Content-Type-Options, Referrer-Policy, and CSP headers
Overview
Separate server-only secrets from NEXT_PUBLIC_ browser variables and configure security headers in next.config.ts.
Next.js App Router unifies server rendering, streaming, type-safe mutations, and edge-ready caching inside a single React + TypeScript codebase.
Production Code Example
// next.config.ts
const nextConfig = {
async headers() {
return [{
source: '/(.*)',
headers: [
{ key: 'X-Content-Type-Options', value: 'nosniff' },
{ key: 'Referrer-Policy', value: 'strict-origin-when-cross-origin' },
],
}];
},
};
export default nextConfig;
Key Takeaways
- Never prefix database passwords or API secrets with NEXT_PUBLIC_
- Use the server-only package to guard secret helper modules at build time
- Configure X-Content-Type-Options, Referrer-Policy, and CSP headers
Quick Check
Check Your Understanding
Score: 0 / 1
1. What happens if you name an API secret `NEXT_PUBLIC_STRIPE_SECRET_KEY` in a Next.js app?
Study Workspace
Finished this lesson?
Your progress is saved locally in your browser, no account needed.