Error: Dynamic server usage: Page couldn't be rendered statically because it used `cookies`
Reading `cookies()`, `headers()`, or `searchParams` inside a page Next.js is trying to statically generate forces that route to render dynamically instead. If that's intended, add `export const dynamic = 'force-dynamic'` to opt in explicitly rather than letting the build warn about it.
What causes this error
Next.js tries to statically generate as many routes as possible at build time for performance. Certain APIs — cookies(), headers(), and reading searchParams in some contexts — depend on the incoming request, which doesn't exist yet at build time. Using them forces that specific route out of the static path.
The Fix
// Explicitly opt this route into dynamic rendering
export const dynamic = 'force-dynamic';
export default async function Page() {
const cookieStore = await cookies();
const theme = cookieStore.get('theme');
// ...
}If you expected this page to be static and it's not supposed to read request data, that's usually the real bug — check for an accidental cookies() or headers() call left over from debugging.
Common causes / variations
- Reading auth cookies on a page that should otherwise be static/cacheable
- Using
headers()to check a user-agent or locale on every request - Not realizing a shared layout or component further down the tree is the one calling these APIs
Related errors
See also: params should be awaited before using its properties.
Comments
Loading comments...