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.


_10
- try {
_10
- const theme = (await cookies()).get('theme')?.value;
_10
- } catch {
_10
- // this also catches the Next.js signal
_10
- }
_10
+ const cookieStore = await cookies();
_10
+ 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.


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

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.


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

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).
#
file under:
Next.js

quick question

Was this resource helpful?