---
title: "Dynamic server usage: Route couldn't be rendered statically because it used cookies"
description: "What the Next.js Dynamic server usage error means, why cookies() and headers() make a route dynamic, and how to fix it without breaking static rendering elsewhere."
url: https://www.danny.engineering/answer/nextjs/dynamic-server-usage-route-couldnt-be-rendered-statically-because-it-used-cookies
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:25:00.000Z
tags: ["Next.js"]
---

# Dynamic server usage: Route couldn't be rendered statically because it used cookies

## The short answer


Calling `cookies()`, `headers()` or reading `searchParams` makes a route depend on the incoming request, so it cannot be built ahead of time. You see this error when something forces the route to be static anyway, or when your code catches the internal signal Next.js uses to detect dynamic rendering. Usually the fix is to stop swallowing that signal, or to remove the setting that forces static output.


```diff
- try {
-   const theme = (await cookies()).get('theme')?.value;
- } catch {
-   // this also catches the Next.js signal
- }
+ const cookieStore = await cookies();
+ const theme = cookieStore.get('theme')?.value ?? 'light';
```


## Why it happens


During `next build`, Next.js tries to prerender each route. When your code calls a request-time API such as `cookies()`, Next.js throws a special internal error to stop prerendering and mark the route as dynamic. Normally you never see it: the route simply renders on each request instead.


The error reaches you when that process is blocked. The common reasons are:

- A `try`/`catch` around the call catches the internal error, so Next.js cannot see that the route is dynamic.
- The route sets `export const dynamic = 'error'` or `'force-static'`, which tells Next.js the route must be static.
- The app uses `output: 'export'`, which produces a fully static site with no server to read cookies.
- The call happens inside `generateStaticParams` or another function that only runs at build time.

Remember that `cookies()` and `headers()` are asynchronous since Next.js 15, so you must `await` them.


## How to fix it


### Let the error pass through


If you need a `try`/`catch` for your own logic, call the request API outside it, or rethrow Next.js errors. The `unstable_rethrow` helper from `next/navigation` rethrows any internal Next.js error and lets you handle the rest.


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

export default async function Page() {
  try {
    const session = (await cookies()).get('session')?.value;
    const user = await getUser(session);
    return <Profile user={user} />;
  } catch (error) {
    unstable_rethrow(error);
    return <p>Could not load your profile.</p>;
  }
}
```


### Decide whether the route should be static


If the page really needs the user's cookies, remove `dynamic = 'error'` or `'force-static'` and accept that it renders per request. If it does not, move the cookie read out of the shared code path, for example out of a layout used by every page. A single `cookies()` call in the root layout makes every route dynamic.


### Read cookies in a smaller piece


Keep most of the page static and push the personalised part into a component wrapped in `Suspense`, or read the value in the browser in a Client Component. With Partial Prerendering or Cache Components, the static shell is served instantly and only the dynamic part waits on the request.


```typescript
import { Suspense } from 'react';

export default function Page() {
  return (
    <>
      <StaticHero />
      <Suspense fallback={<p>Loading your basket...</p>}>
        <Basket /> {/* reads cookies() inside */}
      </Suspense>
    </>
  );
}
```


### For static exports


With `output: 'export'` there is no server at request time. Read cookies in the browser with `document.cookie`, or move the logic into an API you call from the client.


## Common pitfalls

- Wrapping the whole page body in `try`/`catch` for error handling. Use an `error.tsx` file instead, which works with Next.js rather than against it.
- Calling `cookies()` in a shared helper that every page imports, then wondering why nothing is static any more.
- Forgetting to `await`. In Next.js 15 synchronous access was still allowed with a warning; treat it as an error and always await.
- Trying to set cookies in a Server Component. You can only set them in a Server Action, Route Handler or proxy (called middleware before Next.js 16).

## Related reading

- [Next.js: Dynamic Server Error](https://nextjs.org/docs/messages/dynamic-server-error)
- [Next.js: cookies](https://nextjs.org/docs/app/api-reference/functions/cookies)
