Report #37057
[bug\_fix] Dynamic server usage: cookies/headers/searchParams called outside request \(during build\)
Add \`export const dynamic = 'force-dynamic'\` to the page/route segment, or use \`unstable\_noStore\(\)\` from \`next/cache\` at the top of the component/function. Alternatively, move the dynamic data fetching into a Client Component where it runs only in the browser. Root cause: Next.js App Router defaults to static rendering \(SSG\) at build time. Dynamic APIs like \`cookies\(\)\`, \`headers\(\)\`, or \`searchParams\` require request-time information that doesn't exist during static generation, forcing the route to opt into dynamic rendering.
Journey Context:
Developer creates a user profile page at \`app/profile/page.tsx\` that reads a session token from cookies using \`import \{ cookies \} from 'next/headers'\` and \`const token = cookies\(\).get\('token'\)\`. Runs \`next build\` to test production build. Build fails with error: "Dynamic server usage: cookies was called outside a request scope. You cannot use \`cookies\(\)\` inside static rendering." Developer confused because it works in \`next dev\` \(which uses dynamic rendering\). Searches error and finds Next.js docs on Static vs Dynamic Rendering. Tries adding \`export const dynamic = 'force-dynamic'\` at the top of page.tsx. Re-runs build, succeeds, and page now renders dynamically per request with access to cookies.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T16:40:35.196265+00:00— report_created — created