Next.js (App Router)
Next.js App Router — extends the React rules. Server Actions, the server/client boundary, caching and redirects.
Next.js App Router — extends the React rules. Server Actions, the server/client boundary, caching and redirects.
Extends React (web) — those rules apply too; this file only adds what's specific to Next.js (App Router).
Which repositories get it
A repository on this profile: web-nextjs. redline init detects the profile and --profile overrides it — see Profiles & stacks.
Within such a repository, a file gets these rules when it matches one of these globs:
app/**src/app/**pages/**src/pages/**middleware.tssrc/middleware.tsnext.config.tsnext.config.jsnext.config.mjsapps/**/app/**apps/**/src/app/**
The 25 rules
When a review comment cites one of these ids, this is where to look up what it catches. Of the 25, 10 BLOCKER, 11 HIGH and 4 SUGGESTION. A finding from this file opens like this:
Redline/BLOCKER [nextjs/server-action-missing-auth]: <one-line problem>What each severity obliges you to do is on The output contract.
| Rule id | Severity | Catches |
|---|---|---|
nextjs/server-action-missing-auth | BLOCKER | Server Action with no authorisation check. A Server Action compiles to a public HTTP endpoint with a generated id. Being defined in a protected page, imported only by an authenticated component, or hidden behind a rendered condition proves nothing — the endpoint is callable directly, forever, by anyone who has seen the id. Every action re-checks session and ownership in its own body. |
nextjs/secret-in-public-env | BLOCKER | A secret read from a `NEXT_PUBLIC_`-prefixed variable. The prefix inlines the literal value into the client bundle at build time. It is not a runtime lookup and cannot be rotated out of an already-shipped bundle. Anything that is not safe on a billboard uses an unprefixed name and is read on the server. |
nextjs/server-only-leak | BLOCKER | Server-only code reachable from a client component. A module that touches the database, a private SDK, or a server environment variable, imported (directly or transitively) beneath a `'use client'` boundary, ships its source to the browser. Mark such modules `import 'server-only'` so the build fails instead of the secret shipping. |
nextjs/route-handler-unvalidated-input | BLOCKER | Route handler consuming `request.json()`, `formData()` or search params without a schema check. A route handler is an external boundary in the sense core.md means it. Parse into a validated shape before the value reaches a query, a file path, or a response. |
nextjs/middleware-as-sole-auth | BLOCKER | Middleware as the only authorisation. `middleware.ts` runs on a matcher, not on every code path — Server Actions, route handlers invoked directly, and paths the matcher does not cover all bypass it. Treat it as a redirect for humans, never as the enforcement point. |
nextjs/user-data-in-cached-render | BLOCKER | Request-specific data rendered in a cached path. A segment that is statically rendered or wrapped in `unstable_cache` serves one response to everybody. Rendering the signed-in user's name, balance or permissions into it hands the first visitor's data to the second. Opt the segment out, or move the per-user part behind its own dynamic boundary. |
nextjs/unsanitised-redirect | BLOCKER | `redirect()` given a user-controlled destination. A `?next=` or `?returnTo=` parameter passed through unchecked is an open redirect, and in an auth callback it is a token-forwarding gadget. Allow-list the path, or accept a relative path only. |
nextjs/client-side-authorisation | BLOCKER | Authorisation decided in client code. Hiding a control with `session?.role === 'admin'` in a client component is presentation. The same check belongs in the action or handler behind it; without it the bundle simply documents which request to forge. |
nextjs/async-client-component | BLOCKER | An `async` component in a `'use client'` module. Client Components cannot be async, so the route errors at render — typical when `'use client'` is added for a `useState` to a page that still `await`s. Fetch in a Server Component and pass the data down, or pass a promise and unwrap it with `use()`. |
nextjs/cookie-write-in-render | BLOCKER | `cookies().set`/`.delete` while a Server Component renders. Headers are already streaming, so the call throws and every visitor gets a 500. Write cookies in a Server Action, Route Handler or middleware/proxy. |
nextjs/unbounded-revalidate | HIGH | Mutable data fetched without a `revalidate` window or `cache: 'no-store'`, leaving it cached until the next deploy. |
nextjs/client-boundary-too-high | HIGH | `'use client'` on a layout or page root, pulling its whole subtree into the client bundle when a leaf needed it. |
nextjs/sequential-server-fetches | HIGH | Independent `await`s in one Server Component, serialising requests that `Promise.all` would overlap. |
nextjs/missing-suspense-boundary | HIGH | An async Server Component with no `loading.tsx` or `<Suspense>` above it — the whole route waits on its slowest fetch. |
nextjs/missing-error-boundary | HIGH | A route segment that fetches or can throw, with no `error.tsx`. |
nextjs/dynamic-api-in-layout | HIGH | `cookies()`, `headers()` or `searchParams` read in a root layout, opting every route beneath it out of static rendering as a side effect. |
nextjs/raw-img-element | HIGH | `<img>` where `next/image` applies — no sizing, no format negotiation, and a layout shift on every load. |
nextjs/route-handler-no-cache-intent | HIGH | A `GET` route handler with neither an explicit `dynamic` nor a cache directive, whose behaviour then changes silently between Next versions. |
nextjs/action-returns-internal-error | HIGH | A Server Action returning a caught error's `message` or `stack` to the client, putting the query, path or driver detail on the page. |
nextjs/navigation-throw-caught | HIGH | `redirect()`, `notFound()`, `forbidden()` or `unauthorized()` inside a `try` whose `catch` does not rethrow. They work by throwing, so the catch swallows them: the redirect never happens and the action reports failure for a write that succeeded, inviting a duplicate retry. Call them after the `try`, or `unstable_rethrow(err)` first in the catch. |
nextjs/mutation-without-revalidation | HIGH | A Server Action that writes without `revalidatePath`, `revalidateTag`/`updateTag` or `refresh()`. The cached payload still holds the old state, so the user sees the old name after renaming and renames again. Revalidate what the write affects. Not when the action redirects to a dynamic route that refetches. |
nextjs/prefer-server-component | SUGGESTION | A client component whose only reason to be one is data fetching — fetch it on the server and pass the result down. |
nextjs/prefer-link-navigation | SUGGESTION | `router.push` in an `onClick` where `<Link>` expresses the same navigation and prefetches. |
nextjs/prefer-next-font | SUGGESTION | Fonts loaded via a stylesheet `@import` rather than `next/font`, costing a round trip and a layout shift. |
nextjs/missing-route-metadata | SUGGESTION | A public route exporting no `metadata` or `generateMetadata`. |
Changing a rule
Edit standards/stacks/nextjs.md and nowhere else. Every other copy is generated from it. A merged change reaches every onboarded repository as a pull request, so treat it as a production change.
Rule ids are permanent. Reword a rule freely; it keeps its id. Renaming or removing an id orphans every telemetry record that cited it.
- Edit the markdownstandards/ is the only place a human edits a rule. AGENTS.md, CLAUDE.md, .github/copilot-instructions.md and .github/instructions/ are rendered from it, and the next render overwrites any hand edit there.
- node scripts/assign-rule-ids.mjsAssigns a permanent <stack>/<slug> id to any new rule bullet and rewrites the file in place. Do not invent an id by hand. CI runs the same script with --check and fails if a rule is missing one.
- node scripts/render-self.mjsRe-renders this repository's own copies of those files. CI runs it with --check, so a stale copy fails the build.
- Bump standards/manifest.json → versionIn the same pull request as the rule change. Sync pull requests quote the version, so every repository can tell which standards it is on.
- Add a CHANGELOG.md entryAlso in the same pull request. Say what changed and why, and record the seed score: a standards change with no measurement is an opinion.
- node scripts/validate.mjsThe self-check CI runs: the manifest is consistent, rule ids are well formed, the output contract is intact, and every glob is one Copilot can read.
Related
- The output contract — what each severity obliges you to do.
- Profiles & stacks — how a repository ends up with a set of rules.
- Seeded corpus: nextjs — the known-bad code that tests whether reviewers catch these rules.
- All standards
The full file
# Next.js Review Rules
**Scope:** Next.js App Router. These extend the React rules — they do not replace
them. Written for the App Router and Server Components, because that is what new
code starts on; a Pages Router rule appears here only where the two disagree about
whether something is a bug. Do not apply these to plain React or React Native
files.
The framework's defining hazard is that the server/client boundary is invisible in
the source. A file is server code until a `'use client'` somewhere above it says
otherwise, and nothing in the syntax of an import tells a reader which side they
are on. Most rules below are that boundary, seen from a different angle.
## BLOCKER — request changes
- `nextjs/server-action-missing-auth` — **Server Action with no authorisation check.** A Server Action compiles to a public HTTP endpoint with a generated id. Being defined in a protected page, imported only by an authenticated component, or hidden behind a rendered condition proves nothing — the endpoint is callable directly, forever, by anyone who has seen the id. Every action re-checks session and ownership in its own body.
```tsx
// WRONG — the page checked, the action did not
export async function deleteInvoice(id: string) {
'use server';
await db.invoice.delete({ where: { id } });
}
// RIGHT
export async function deleteInvoice(id: string) {
'use server';
const session = await auth();
if (!session) throw new Error('unauthorised');
await db.invoice.delete({ where: { id, userId: session.user.id } });
}
```
- `nextjs/secret-in-public-env` — **A secret read from a `NEXT_PUBLIC_`-prefixed variable.** The prefix inlines the literal value into the client bundle at build time. It is not a runtime lookup and cannot be rotated out of an already-shipped bundle. Anything that is not safe on a billboard uses an unprefixed name and is read on the server.
- `nextjs/server-only-leak` — **Server-only code reachable from a client component.** A module that touches the database, a private SDK, or a server environment variable, imported (directly or transitively) beneath a `'use client'` boundary, ships its source to the browser. Mark such modules `import 'server-only'` so the build fails instead of the secret shipping.
- `nextjs/route-handler-unvalidated-input` — **Route handler consuming `request.json()`, `formData()` or search params without a schema check.** A route handler is an external boundary in the sense core.md means it. Parse into a validated shape before the value reaches a query, a file path, or a response.
- `nextjs/middleware-as-sole-auth` — **Middleware as the only authorisation.** `middleware.ts` runs on a matcher, not on every code path — Server Actions, route handlers invoked directly, and paths the matcher does not cover all bypass it. Treat it as a redirect for humans, never as the enforcement point.
- `nextjs/user-data-in-cached-render` — **Request-specific data rendered in a cached path.** A segment that is statically rendered or wrapped in `unstable_cache` serves one response to everybody. Rendering the signed-in user's name, balance or permissions into it hands the first visitor's data to the second. Opt the segment out, or move the per-user part behind its own dynamic boundary.
- `nextjs/unsanitised-redirect` — **`redirect()` given a user-controlled destination.** A `?next=` or `?returnTo=` parameter passed through unchecked is an open redirect, and in an auth callback it is a token-forwarding gadget. Allow-list the path, or accept a relative path only.
- `nextjs/client-side-authorisation` — **Authorisation decided in client code.** Hiding a control with `session?.role === 'admin'` in a client component is presentation. The same check belongs in the action or handler behind it; without it the bundle simply documents which request to forge.
- `nextjs/async-client-component` — **An `async` component in a `'use client'` module.** Client Components
cannot be async, so the route errors at render — typical when `'use client'` is added for a `useState` to a
page that still `await`s. Fetch in a Server Component and pass the data down, or pass a promise and unwrap it
with `use()`.
- `nextjs/cookie-write-in-render` — **`cookies().set`/`.delete` while a Server Component renders.** Headers are
already streaming, so the call throws and every visitor gets a 500. Write cookies in a Server Action, Route
Handler or middleware/proxy.
## HIGH
- `nextjs/unbounded-revalidate` — Mutable data fetched without a `revalidate` window or `cache: 'no-store'`, leaving it cached until the next deploy.
- `nextjs/client-boundary-too-high` — `'use client'` on a layout or page root, pulling its whole subtree into the client bundle when a leaf needed it.
- `nextjs/sequential-server-fetches` — Independent `await`s in one Server Component, serialising requests that `Promise.all` would overlap.
- `nextjs/missing-suspense-boundary` — An async Server Component with no `loading.tsx` or `<Suspense>` above it — the whole route waits on its slowest fetch.
- `nextjs/missing-error-boundary` — A route segment that fetches or can throw, with no `error.tsx`.
- `nextjs/dynamic-api-in-layout` — `cookies()`, `headers()` or `searchParams` read in a root layout, opting every route beneath it out of static rendering as a side effect.
- `nextjs/raw-img-element` — `<img>` where `next/image` applies — no sizing, no format negotiation, and a layout shift on every load.
- `nextjs/route-handler-no-cache-intent` — A `GET` route handler with neither an explicit `dynamic` nor a cache directive, whose behaviour then changes silently between Next versions.
- `nextjs/action-returns-internal-error` — A Server Action returning a caught error's `message` or `stack` to the client, putting the query, path or driver detail on the page.
- `nextjs/navigation-throw-caught` — **`redirect()`, `notFound()`, `forbidden()` or `unauthorized()` inside a
`try` whose `catch` does not rethrow.** They work by throwing, so the catch swallows them: the redirect never
happens and the action reports failure for a write that succeeded, inviting a duplicate retry. Call them after
the `try`, or `unstable_rethrow(err)` first in the catch.
- `nextjs/mutation-without-revalidation` — **A Server Action that writes without `revalidatePath`,
`revalidateTag`/`updateTag` or `refresh()`.** The cached payload still holds the old state, so the user sees
the old name after renaming and renames again. Revalidate what the write affects. Not when the action
redirects to a dynamic route that refetches.
## SUGGESTION
- `nextjs/prefer-server-component` — A client component whose only reason to be one is data fetching — fetch it on the server and pass the result down.
- `nextjs/prefer-link-navigation` — `router.push` in an `onClick` where `<Link>` expresses the same navigation and prefetches.
- `nextjs/prefer-next-font` — Fonts loaded via a stylesheet `@import` rather than `next/font`, costing a round trip and a layout shift.
- `nextjs/missing-route-metadata` — A public route exporting no `metadata` or `generateMetadata`.