Free rules / Next.js 16
Next.js 16 Rules for AI Coding (free)
AI coding tools learned Next.js from years of version 13-15 examples, so in a Next.js 16 app they still write synchronous params, middleware.ts, revalidateTag with one argument and next lint. This free rules file tells your agent what changed in 16, which config keys are gone, and where security headers belong.
Old code agents still write, and what Next.js 16 wants
| Instead of | Use | Why |
|---|---|---|
const { slug } = params | const { slug } = await props.params | Sync access to params, searchParams, cookies(), headers() and draftMode() is fully removed in 16 |
middleware.ts + export function middleware | proxy.ts + export function proxy | middleware is deprecated; proxy runs on Node.js only, so edge middleware should stay as it is |
revalidateTag('posts') | revalidateTag('posts', 'max') | The one-argument form is deprecated and is a TypeScript error; use updateTag() for read-your-writes in Server Actions |
next lint / eslint: {} in next.config | eslint . (ESLint CLI, flat config) or Biome | next lint and the eslint config key are removed; next build no longer lints |
experimental: { turbopack: {...} } | turbopack: {...} (top level) | Turbopack is the default for dev and build; --turbopack is no longer needed |
unstable_cacheLife / unstable_cacheTag | cacheLife / cacheTag | Stable now, imported from next/cache |
serverRuntimeConfig / getConfig() | process.env / NEXT_PUBLIC_* | Runtime config is removed |
images: { domains: [...] } | images: { remotePatterns: [...] } | domains is deprecated; image defaults also changed (qualities [75], 4-hour minimumCacheTTL) |
How to use it
- Cursor: save the file below as
.cursor/rules/nextjs-16.mdc. It attaches to your.ts,.tsx,.jsand.mjsfiles. - Claude Code, Codex and others: paste everything below the
---header intoCLAUDE.mdorAGENTS.mdin your repo root. - Ask the agent to check existing code against it, e.g. “Find sync params/cookies access, middleware.ts and one-argument revalidateTag calls and list the fixes before editing.”
The rules file
---
description: Next.js 16 rules (App Router): async request APIs, proxy.ts, caching APIs, removed config, security headers
globs: **/*.ts, **/*.tsx, **/*.js, **/*.jsx, **/*.mjs
alwaysApply: false
---
# Next.js 16 rules
This project uses Next.js 16 (checked against the official Next.js 16 upgrade guide and the latest 16.x release, 16.4, on 2026-11-09).
Most code examples online are Next.js 13-15. When unsure whether an API or config key still exists, read the docs bundled in `node_modules/next/dist/docs/` or https://nextjs.org/docs instead of guessing.
## Versions
- Node.js 20.9 or newer, TypeScript 5.1 or newer. Don't suggest Node.js 18.
- Don't add `--turbopack` or `--turbo` to scripts: Turbopack is already the default for `next dev` and `next build`.
## Request APIs are async only
- Always `await cookies()`, `await headers()`, `await draftMode()`.
- In pages, layouts, route handlers, `default.js` and metadata image files, `params` is a Promise: `const { slug } = await props.params`. In pages, `searchParams` is a Promise too.
- Use the generated helper types (`PageProps<'/blog/[slug]'>`, `LayoutProps`, `RouteContext`; run `npx next typegen`) instead of hand-written prop types.
- `opengraph-image`, `twitter-image`, `icon` and `apple-icon` functions get `params` and `id` as Promises: await them. `sitemap({ id })` gets `id` as a Promise<string>. `generateImageMetadata` still gets plain `params`.
- In Client Components that receive these Promises, unwrap them with React's `use()`.
## proxy.ts, not middleware.ts
- New request interception goes in `proxy.ts` (or `proxy.js`) and the exported function is named `proxy`. The `middleware` file name and export are deprecated.
- `proxy` always runs on the Node.js runtime and can't be configured. If the existing middleware needs the edge runtime, keep `middleware.ts` and say so; never rename it silently.
- Config flags with "middleware" in the name are renamed (for example `skipMiddlewareUrlNormalize` is now `skipProxyUrlNormalize`).
## Caching APIs
- `revalidateTag(tag, profile)` needs the second argument, for example `revalidateTag('posts', 'max')`. The one-argument form is deprecated.
- In a Server Action where the user must see their own change right away, use `updateTag(tag)` (Server Actions only). Use `refresh()` from `next/cache` to refresh the client router from a Server Action.
- Import `cacheLife` and `cacheTag` from `next/cache` without the `unstable_` prefix.
- Don't add `experimental.ppr`, `experimental_ppr`, `experimental.dynamicIO` or `experimental.useCache`: they are removed. `cacheComponents: true` is a different model, not a rename; don't turn it on just to finish an upgrade.
- Don't turn on `reactCompiler` just to finish an upgrade either; it is opt-in and slows builds.
## Config that no longer exists
- No `next lint` (removed) and no `eslint` key in next.config. Lint with the ESLint CLI (flat config) or Biome. `next build` no longer lints.
- No `serverRuntimeConfig`, `publicRuntimeConfig` or `getConfig()`. Use `process.env` on the server and `NEXT_PUBLIC_` variables for the client.
- Turbopack options go in top-level `turbopack`, not `experimental.turbopack`.
- A custom `webpack` config makes `next build` fail. Ask before switching the build to `--webpack`.
- No AMP (`next/amp`, `amp` config): removed.
- Every parallel route slot (`@slot`) needs a `default.js` that returns `null` or calls `notFound()`, or the build fails.
- Don't add `experimental.viewTransition` (removed in 16.3) or `experimental.appNewScrollHandler` (removed in 16.4).
## next/image
- Use `images.remotePatterns`, not `images.domains` (deprecated). Use `next/image`, not `next/legacy/image`.
- Defaults changed: `qualities` is `[75]` (other `quality` values are coerced to the closest allowed one), `minimumCacheTTL` is 4 hours, `16` is gone from `imageSizes`, at most 3 redirects, local IPs are blocked.
- A local `src` with a query string (`/photo.png?v=1`) needs a matching `images.localPatterns` entry with `search`.
## Security headers
- Set static security headers in `next.config` `headers()` with `source: '/(.*)'`, not in `proxy.ts`. Only use `proxy.ts` for a CSP if the app needs per-request nonces, and say that nonces force dynamic rendering.
- In development React needs `'unsafe-eval'` in `script-src`; never ship `'unsafe-eval'` to production.
- Use CSP `frame-ancestors` against clickjacking (it supersedes `X-Frame-Options`; keeping both is fine). `X-Content-Type-Options` only takes `nosniff`.
- After changing headers, check the deployed site's response headers, not just the build.
## Upgrading an older app
- Official helpers: `npx @next/codemod@canary upgrade latest`, then `npx @next/codemod@canary next-async-request-api .` (the upgrade codemod doesn't run that one). `npx @next/codemod@canary agents-md` points AGENTS.md at version-matched docs.
- After a codemod, search for leftover `UnsafeUnwrapped` casts and codemod error comments and fix them.
- A green build is not the finish line: run the app's tests and click through the routes the change touched.
Security headers in Next.js 16
Static security headers go in the headers() function in next.config, not in proxy.ts. This is the no-nonce setup from the Next.js docs, which keeps pages static. If you need a per-request CSP nonce, that does go in proxy.ts, and the docs note that nonces require dynamic rendering.
// next.config.ts — static security headers on every route (no nonces, page stays static)
import type { NextConfig } from 'next'
const isDev = process.env.NODE_ENV === 'development'
const csp = [
"default-src 'self'",
`script-src 'self' 'unsafe-inline'${isDev ? " 'unsafe-eval'" : ''}`,
"style-src 'self' 'unsafe-inline'",
"img-src 'self' blob: data:",
"font-src 'self'",
"object-src 'none'",
"base-uri 'self'",
"form-action 'self'",
"frame-ancestors 'none'",
'upgrade-insecure-requests',
].join('; ')
const nextConfig: NextConfig = {
async headers() {
return [
{
source: '/(.*)',
headers: [
{ key: 'Content-Security-Policy', value: csp },
{ key: 'Strict-Transport-Security', value: 'max-age=63072000; includeSubDomains; preload' },
{ key: 'X-Content-Type-Options', value: 'nosniff' },
{ key: 'Referrer-Policy', value: 'strict-origin-when-cross-origin' },
{ key: 'Permissions-Policy', value: 'camera=(), microphone=(), geolocation=()' },
],
},
]
},
}
export default nextConfig'unsafe-eval'is only added in development; the Next.js CSP guide says React needs it there and production doesn't.'unsafe-inline'inscript-srcis the trade-off for skipping nonces. Add any outside domains you load (analytics, Stripe, fonts) to the matching directive, or those requests get blocked.- Only add
includeSubDomains; preloadto HSTS if every subdomain serves HTTPS. - Check it after you deploy:
curl -sI https://your-site.example | grep -i -E "content-security|strict-transport|x-content-type", then open the browser console and look for CSP errors.
Sources, checked Nov 9, 2026: Next.js headers() reference (header examples and values) and Content Security Policy guide (“Without Nonces” config, proxy nonces).
Common questions
Why does my AI keep writing middleware.ts in Next.js 16?
Most examples it learned from are Next.js 12-15, where middleware.ts was the only option. In Next.js 16 the file and export are renamed to proxy (proxy.ts, export function proxy). Proxy runs on the Node.js runtime only, so if your existing middleware needs the edge runtime, the docs say to keep middleware for now instead of renaming it.
What does the error about params being a Promise mean?
Next.js 15 allowed synchronous access to params, searchParams, cookies(), headers() and draftMode() for compatibility. Next.js 16 removed it, so you must await them: const { slug } = await props.params. The official codemod is npx @next/codemod@canary next-async-request-api . and npx next typegen generates PageProps, LayoutProps and RouteContext types.
Where should security headers go in Next.js 16: next.config or proxy.ts?
For static headers (HSTS, X-Content-Type-Options, a CSP without nonces), use the headers() function in next.config with source '/(.*)'. Use proxy.ts only if you need a fresh CSP nonce per request; the Next.js CSP guide notes that nonces require dynamic rendering.
Is there an official way to give my agent the right Next.js docs?
Yes. The upgrade guide recommends npx @next/codemod@canary agents-md, which points AGENTS.md at version-matched docs bundled in node_modules/next/dist/docs/. On Next.js 16.2 and later, next dev keeps that block up to date when it detects a coding agent. These rules are a short summary on top of that, not a replacement.
Sources: the official Next.js 16 upgrade guide, the headers() reference and the CSP guide, all read on Nov 9, 2026. Where the docs give no number, this page doesn't either.
Want a Next.js 16 app that already follows these rules? The SaaS Starter Cartridge ($49) is a working Next.js 16 app with sign-in, Stripe subscriptions, tests and these rules built in.
Check your deployed headers with ShipCheck's free scan (sister project)