Module P-10·27 min read

The complete next.config.ts surface, plus the NEXT_PUBLIC_ build-time baking trap (why the same Docker image cannot serve staging and production), runtime environment injection patterns, and the instrumentation.ts startup hook.

JJS
Written by Jatin Jain Saraf · Senior Software Engineer

P-10 — Configuration: next.config.ts, Environment Variables, and Instrumentation

Who this is for: Engineers who copy-paste next.config.js from the docs, tweak a few things, and hope for the best. This module covers the full configuration surface of Next.js 15 — what each knob does, when you actually need it, and the patterns that separate a production-hardened config from a default one.


next.config.ts — TypeScript Is Now the Default

For most of Next.js's history, the configuration file was next.config.js — plain JavaScript with a CommonJS export. As of Next.js 15, next.config.ts is the default. This matters because you get autocomplete and type checking on every configuration option, which means you catch mistakes before running next build rather than discovering them at 2am.

ts

If you have an existing .js config, migrating to .ts is typically a rename plus swapping module.exports for export default. The occasional gotcha is plugins that wrap your config with a higher-order function — check that the plugin you're using exports proper TypeScript types. Most major ones (@next/bundle-analyzer, next-intl, @sentry/nextjs) do.


Redirects: Permanent, Temporary, and Conditional

Redirects belong in next.config.ts when they are structural — old URLs that have permanently moved, API endpoints that have been renamed, or paths you want to consolidate. They run at the routing layer, before any page or layout renders, so they're fast and require no JavaScript execution.

ts

The has and missing matchers let you make redirects conditional on headers, cookies, or query parameters. The last example redirects unauthenticated users away from the admin area at the config level — before Middleware even runs. Be careful with this approach: it's fast but it's also static and can't handle JWTs or complex session validation. Use Middleware for anything that needs actual logic.

permanent: true sends a 308 (Moved Permanently for non-GET methods) or 301 depending on the context. permanent: false sends a 307. The difference matters for SEO — permanent redirects transfer link equity, temporary ones don't. If you are genuinely moving a URL forever, use permanent: true. If you might change your mind, use permanent: false.


Rewrites: Proxying Without Redirecting

Rewrites change what Next.js renders without changing the URL the user sees. This is useful for proxying requests to external services, hiding implementation details, or maintaining URL stability during a migration.

ts

beforeFiles rewrites are powerful and dangerous in equal measure. They run before Next.js even checks whether a page file exists, which means a rewrite can shadow a real page. Use them intentionally. fallback rewrites are the cleanest way to do an incremental migration: new pages live in Next.js, unknown paths fall through to the old system.


Security Headers

The headers() function is where you add HTTP security headers to every response. This is one of those things that separates "shipped" from "production-hardened":

ts

CSP (Content-Security-Policy) is the most powerful but also the most likely to break things. The unsafe-inline and unsafe-eval entries are pragmatic defaults for Next.js apps that use inline styles and the React hydration script — tighten them gradually as you audit your app. X-Frame-Options: SAMEORIGIN prevents clickjacking. HSTS with preload tells browsers to only connect over HTTPS and can be submitted to the HSTS preload list.

Run your deployed app through securityheaders.com after deploying. It grades your header configuration and tells you what's missing.


Image Configuration

The images config is where you whitelist external image domains and tune format support:

ts

Sign in to keep reading

The rest of this module is free — sign in with Google to unlock it and track your progress.

Sign in & Register

Discussion

0

Join the discussion

Loading comments...

© 2026 Jatin Jain Saraf (JJS). All rights reserved.