Next.js 16 rules for AI coding agents: what they get wrong and how to fix it
On this page
Next.js moves faster than model training data. Agents trained mostly on Next.js 13–15 code still reach for APIs that Next.js 16 deprecated, and they rarely know about the cache behavior that matters most for multi-tenant apps.
These are the rules in our Next.js preset, why each exists, and the official doc behind it. Copy the ideas into your own rules, or install the preset.
npx presets-dev add nextjs1. proxy.ts, not middleware.ts
In Next.js 16 the middleware file convention is deprecated and renamed to proxy, and Proxy defaults to the Node.js runtime (Next.js docs). Agents still create middleware.ts by reflex.
`middleware.ts` is the deprecated name on Next 16. Write `proxy.ts` exporting `proxy`, unless the app must stay on the Edge runtime.
The exception matters. A rule that says "never write middleware.ts" will make an agent break an app that depends on Edge.
2. revalidateTag takes two arguments now
The one-argument revalidateTag(tag) is deprecated; the docs say to "update to the two-argument signature" (Next.js docs). There's also a newer function, updateTag, built for read-your-own-writes and only callable from Server Actions (Next.js docs).
Our rule tells the agent which one to pick:
updateTag(tag)in a Server Action when the user must see their own change immediately.revalidateTag(tag, 'max')when a stale response is acceptable, or when the caller is a Route Handler (whereupdateTagcan't run).
3. images.remotePatterns, not images.domains
images.domains is deprecated in favor of images.remotePatterns (Next.js docs). It's a one-line rule, and it saves a review comment on almost every image change an agent makes.
4. Cache keys isolate tenants; tags don't
This is the rule we spent the most time on. With 'use cache', the cache key is built from the function's arguments and any outer variables it captures (Next.js docs). A tag from cacheTag is only how you invalidate. It doesn't separate one tenant's data from another's.
So the leak isn't "a shared tag". It's a key that leaves out the tenant while the cached value holds that tenant's rows. Pass the tenant in, so it becomes part of the key:
import { cacheTag } from 'next/cache'
export async function getNotes(tenantId: string) {
'use cache'
cacheTag(`tenant:${tenantId}:notes`)
return db.note.findMany({ where: { tenantId } })
}
Cached functions can't call cookies() or headers(). The docs say to read them outside and pass the values in as arguments, which is exactly what makes the key correct.
This isn't hypothetical. The November 2026 Next.js security release fixed a high-severity cross-tenant cache disclosure when combining unstable_cache and use cache (Next.js blog). Our rule tells the agent not to wrap the same public cached function in unstable_cache, and to check the installed version: on 16.x below 16.4.5, or 15.5.x below 15.5.29, it says so before adding cached functions.
5. A Server Action is a public endpoint
Anyone can call a Server Action with any payload. Agents tend to treat them like internal functions and trust the form. Our rule, as a worked example:
'use server'
import { updateTag } from 'next/cache'
export async function addNote(formData: FormData) {
const session = await requireSession()
const body = String(formData.get('body') ?? '').trim().slice(0, 2000)
if (!body) return { ok: false, error: 'Note is empty' }
await db.note.create({
data: { tenantId: session.tenantId, authorId: session.userId, body },
})
updateTag(`tenant:${session.tenantId}:notes`)
return { ok: true }
}
In order: session first, user and tenant from the session (never from the form), validated and capped input, then updateTag on the same tag the reader used. requireSession and db stand in for your app's own helpers.
6. Use the docs that ship with next
Since Next.js 16.2, the next package bundles docs for agents and points to them from a managed block in AGENTS.md, and the dev server exposes an MCP endpoint (Next.js AI agents guide). Our rule tells the agent to use them when they're installed. They match your exact version, which no rule file can.
The review skill
Rules shape every edit. For a pre-merge check, the preset includes a nextjs-code-review skill that walks through structure, Server Actions, data fetching and caching, security, and tests, and reports findings with file and line. It never edits files unless you ask.
Browse both halves for your tool:
- Next.js rules for Cursor
- Next.js instructions for Copilot
- Next.js code review skill for Claude Code
- Next.js code review skill for Codex
Write your own
The pattern behind every rule above: name the deprecated thing, name the replacement, state the exception, link the doc. More in Anatomy of a good rule.