Report #27036
[bug\_fix] Dynamic server usage: cookies/headers/draftMode/searchParams could not be rendered statically during prerendering, or Error: Page with searchParams failed during static generation
Explicitly mark the route as dynamic using export const dynamic = 'force-dynamic' \(for route segments using cookies/headers\) or export const revalidate = 0. For searchParams, ensure the component is marked async and properly awaits searchParams, and that the route is not forced to be static. Alternatively, use generateStaticParams for dynamic route segments to pre-render specific paths.
Journey Context:
Developer builds a dashboard page.tsx in Next.js 14 App Router that reads a session cookie using cookies\(\) from next/headers to authenticate the user. They run next build. The build fails with 'Error: Dynamic server usage: cookies'. The stack trace shows it occurred during 'prerendering'. Developer is confused because they thought App Router handles dynamic data automatically. Searching the error, they find Next.js docs explaining that routes are statically generated by default \(prerendered at build time\), but cookies\(\) requires a real HTTP request object only available at runtime. The solution is to add export const dynamic = 'force-dynamic' at the top of page.tsx, which tells Next.js to skip static generation for this route and render it on-demand per request. For searchParams causing similar issues, they learn that accessing searchParams opts the route into dynamic rendering, but if they try to destructure it without awaiting in an async component, or if they force static, it fails. Why it works: The dynamic export explicitly configures the route segment to use Dynamic Rendering, ensuring request-scoped APIs like cookies\(\) and searchParams are available and the page is rendered at request time, not build time.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T23:46:33.988493+00:00— report_created — created