Error fixes / Next.js

Next.js

Next.js: DynamicServerError while reading cookies or headers

cookies() or headers() ran outside the request's async context, or broke a static render.

The error

DynamicServerError

What it means

Next.js throws DynamicServerError when cookies() or headers() is not bound to the request that rendered the page. The official page describes calling them inside setTimeout, setInterval, or after a promise you did not await. While static pages are generated, Next.js can also catch this error to switch the page to dynamic rendering. If it is uncaught, the build fails. Generated code often wraps cookies() in a helper that fires without awaiting.

Fix

  1. Find cookies() and headers() calls. Await them directly in the server component or route, which is the pattern the docs show.
  2. Do not call them inside setTimeout or after a promise that is not awaited.
  3. If the page is meant to be static, remove the cookie read. A cookie means the response depends on the user.
  4. If the page is meant to be dynamic, read the cookie in the page itself and let the route be dynamic. Do not force static generation on it.
  5. Run the production build. A dev refresh will not show the static generation failure.

Prompt

Fix prompt
Paste into Cursor or Claude Code
The build throws DynamicServerError around cookies() or headers().

Follow the Next.js guidance: call cookies() or headers() directly in the server component and await the promise. Remove any call inside setTimeout or an un-awaited promise. If this page should be static, delete the cookie read. If it should vary by user, allow dynamic rendering. Do not silence the error with a blank catch. Show me the call site you changed.

Questions

Why does a cookie break a static page?

Static HTML is the same for every visitor. A cookie is different per visitor, so Next.js has to render the route per request. That is allowed, but it has to be intentional.

I await cookies() and it still fails. Where else should I look?

A helper may call it, then return a promise you do not await, or a timer may call it later. The docs say the call has to stay on the same async context as the render.

Can I cache the cookie value in a global?

No. A module-level value is shared between visitors on the server. Read it inside the request.

Related fixes

Source: Next.js docs: Dynamic server error. Updated 14 October 2026.