# Error: Dynamic server usage: Route /x couldn't be rendered statically because it used `headers`. (Next.js)

Printed in next build output, or in server logs, with digest DYNAMIC_SERVER_USAGE while Next.js prerenders App Router routes. Next throws it on purpose to bail out of static rendering; you usually see it because your own code caught and logged it.

```
   Generating static pages (12/24)
Error: Dynamic server usage: Route /account couldn't be rendered statically because it used `headers`. See more info here: https://nextjs.org/docs/messages/dynamic-server-error
    at q (/app/.next/server/chunks/638.js:1:42915)
    at s (/app/.next/server/app/account/page.js:1:3102)
    at async getSession (/app/.next/server/app/account/page.js:1:3540) {
  description: "Route /account couldn't be rendered statically because it used `headers`. See more info here: https://nextjs.org/docs/messages/dynamic-server-error",
  digest: 'DYNAMIC_SERVER_USAGE'
}
```

## Quick fix

Tell Next.js the route is dynamic: export const dynamic = 'force-dynamic' from the page or layout, or await connection() before the request-specific code. If a try/catch wraps headers(), cookies() or a no-store fetch, call unstable_rethrow(err) first in the catch so Next's own bailout error passes through.

1. Note the route and the API named in the message (headers, cookies, searchParams, no-store fetch).
2. Search that route's server code, including shared helpers, for try/catch around those calls.
3. Call unstable_rethrow(err) as the first line of each such catch.
4. If the page should be dynamic, export const dynamic = 'force-dynamic' or await connection().
5. If it should stay static, move the request-specific read into a client component or middleware.
6. With Cache Components enabled, wrap the dynamic part in <Suspense> instead of using segment config.
7. Rebuild and confirm the route shows as dynamic (ƒ) in the build summary.

```ts
import { headers } from 'next/headers';
import { unstable_rethrow } from 'next/navigation';

// Opt this route out of static prerendering
export const dynamic = 'force-dynamic';

export default async function AccountPage() {
  try {
    const h = await headers();
    return <p>Country: {h.get('x-country') ?? 'unknown'}</p>;
  } catch (err) {
    unstable_rethrow(err); // lets Next.js internal errors through
    return <p>Could not read the request.</p>;
  }
}
```

## Why it happens

### A try/catch swallows the bailout signal

Next.js marks a route as dynamic by throwing a special error from headers(), cookies() and similar APIs during prerendering. A try/catch around that call, or around a helper that makes it, catches the signal; logging it prints this message.

### Shared code reads request data

A layout or utility used everywhere (session lookup, locale detection, feature flags) calls cookies() or headers(), so pages you meant to prerender can no longer be static. Auth helpers in libraries often read cookies internally.

### Uncached fetch in a static route

fetch with cache: 'no-store' or next: { revalidate: 0 } opts the route into dynamic rendering. The message then names the fetch instead of headers, but the fix is the same decision: dynamic route or cached data.

### Segment config that contradicts the code

With dynamic = 'error', the build fails as soon as a dynamic API is used. With 'force-static', headers() and cookies() return empty values, which can hide the problem until production behaves strangely.

### Dynamic call in a branch that skipped the build

A cookies() call that only runs for some users or flags never executed during prerendering, so the page was built as static. When that branch runs later during revalidation, Next reports the page changed from static to dynamic at runtime.

## Other fixes

- Read request data in middleware (proxy.ts in Next 16) and rewrite to a path segment, so each variant can still be prerendered.
- Move per-user UI into a client component that fetches after load, keeping the surrounding page static.
- Call auth or session helpers only from dynamic routes, route handlers or server actions.

## Same error, other wording

- `Error: Dynamic server usage: Route /x couldn't be rendered statically because it used `cookies`.`
- `Error: Page changed from static to dynamic at runtime /x, reason: headers`
- `Error: Dynamic server usage: cookies`

## Sources

- [Next.js: Dynamic Server Error](https://nextjs.org/docs/messages/dynamic-server-error)
- [Next.js: dynamic segment config](https://nextjs.org/docs/app/guides/caching-without-cache-components#route-segment-config)
- [Next.js: headers()](https://nextjs.org/docs/app/api-reference/functions/headers)

---

Next.js throws this during the build and catches it itself, so unless your own code catches and reports it Runheed doesn't record this one. Runheed records the exceptions a running app throws and serves them to coding agents over MCP: https://runheed.com/?ref=agent-md

Human-readable version: https://runheed.com/errors/dynamic-server-usage
