Next.js DynamicServerError: Dynamic Server Usage
Next.js throws this at build time when a request-time function like cookies() or headers() runs outside the call stack it was bound to, for example inside setTimeout or after an un-awaited promise. Call those functions directly in the render path and await every promise that uses them.
Cause: cookies()/headers() called inside a new execution context (timer, callback, un-awaited promise), so Next.js can't catch it to opt the page into dynamic rendering.
How to fix it
- Find the route named in the build log and the
cookies()/headers()call it uses. - Move the call out of
setTimeout,setInterval, or callbacks into the main async function. - Await every promise whose work calls these functions.
- Avoid try/catch blocks that swallow errors from these calls during rendering.
Example
Illustrative example, not from a customer project.
async function getCookieData() {
+ const cookieStore = await cookies()
return new Promise((resolve) => setTimeout(async () => {
- const cookieStore = await cookies()
resolve(cookieStore.getAll())
}, 1000))
}
FAQ
Why only during next build?
Static generation happens at build time; normally Next.js catches this error to switch the page to dynamic rendering, but an escaped one fails the build.
Is this the same as a page being dynamic?
No. Using cookies() correctly just makes the page dynamic; this error means the call escaped its async context.
Get a $149 diagnosis See pricing Launching soon? The Launch Path gets you ready for $1,195 flat.
- Fixed, or we keep working free until it is (within the written scope).
- A human reviews every fix before it ships.
- read-only access; fixes arrive as pull requests you merge
- Your $149 diagnosis is credited in full toward any paid job.