Fix guides · Next.js, Lovable, Bolt, v0, Replit, Vercel ·
Fix "headers was called outside a request scope" in Next.js App Router
`headers` was called outside a request scope. Read more: https://nextjs.org/docs/messages/next-dynamic-api-wrong-context
Next.js throws "headers was called outside a request scope" (with a link to the next-dynamic-api-wrong-context docs) when headers() or cookies() runs in a place without an HTTP request. This happens when a Server Component calls headers()/cookies() during static prerendering or in generateStaticParams. The fix depends on the cause: make the route dynamic with `force-dynamic` or `connection()`, or move the call into a request-scoped Server Component/Route Handler/Server Action. A separate build-time error occurs when a Client Component imports next/headers; the fix is to remove that client-side import.
Common causes – and how to tell which one you have
1. Client Component (or its import) calls headers()/cookies()
How to tell: The build fails with a message indicating a Client Component imports next/headers (which requires a Server Component context). The stack trace points to the file with `use client` or a module it imports. This is a separate build-time check; the "headers was called outside a request scope" error itself is a runtime error from Server Components during static generation.
Fix: Move the headers()/cookies() logic into a Server Component, Route Handler, or Server Action. Remove the call from the Client Component and from any shared module it imports.
2. headers() called during static prerendering or generateStaticParams
How to tell: The error appears when a Server Component calls headers()/cookies() during static generation or in generateStaticParams where no HTTP request exists.
Fix: Add `export const dynamic = 'force-dynamic'` at the top of the page/layout/route to make it fully dynamic, OR call `await connection()` (from `next/server`) before headers()/cookies() to opt the route into dynamic rendering. With Partial Prerendering, wrap the component in `<Suspense>` so Next.js streams it at request time. Without PPR, connection() makes the route dynamic as well; there is no way to keep the rest of the route static.
3. Middleware uses next/headers instead of the request object
How to tell: The error comes from middleware.ts and the code imports headers()/cookies() from `next/headers`. Middleware receives a NextRequest object with .headers and .cookies properties.
Fix: Replace `import { headers } from 'next/headers'` with `request.headers` and `request.cookies` directly on the NextRequest parameter. Example: `const cookie = request.cookies.get('name');`
How to fix it
- Copy the exact error from the terminal or build log. It reads: "`headers` was called outside a request scope" (cookies() produces "`cookies` was called outside a request scope"). Read more: https://nextjs.org/docs/messages/next-dynamic-api-wrong-context
- Find the file and line in the stack trace. Open that file and note: (a) is it a Client Component (`use client` at the top)? (b) is it a Server Component that runs during static prerendering? (c) is it Middleware?
- If the call is in a Client Component (or a module it imports): move the headers()/cookies() call into a Server Component, Route Handler, or Server Action. If the module must be shared, add `import 'server-only'` at its top. Next.js will then error at build time if a Client Component imports it.
- If the call runs during static prerendering (static page, generateStaticParams, or a layout without dynamic markers): either add `export const dynamic = 'force-dynamic'` at the top of the route/page to make the whole route dynamic, OR call `await connection()` (from `next/server`) before headers()/cookies() to opt the route into dynamic rendering. With Partial Prerendering, wrapping the component in `<Suspense>` lets Next.js stream that part at request time. Without PPR, connection() makes the route dynamic as well; there is no way to keep the rest of the route static.
- If the call is in Middleware: use `request.headers` and `request.cookies` (the NextRequest object) instead of next/headers. The next/headers API is for Server Components and Route Handlers.
- If the call is in a Route Handler or Server Action but still fails: ensure the handler is actually being invoked by a request (not called directly from a non-request context). In a Server Action, cookies() can also write (set/delete); headers() is read-only.
- After the fix, rebuild and verify the error is gone. For static pages, check that the route is no longer prerendered (or that headers() is only hit at request time).
Using Lovable, Bolt or v0?
In Lovable or Bolt, tell the AI: "The app throws 'headers was called outside a request scope'. Find the call and its context. If it's in a Client Component or imported by one, move it to a Server Component/Server Action and add 'server-only' to the module. If it's during static prerendering, add 'force-dynamic' to the route or call 'connection()' before headers(). If it's in Middleware, use request.headers/request.cookies instead of next/headers."
Using Cursor, Claude Code or Windsurf?
Ask Cursor or Claude Code to locate the exact headers()/cookies() call from the stack trace, identify whether it's in a Client Component (check for 'use client'), a statically rendered Server Component, or Middleware. Have it produce the minimal fix (server-only import, force-dynamic, connection(), or request.headers) and then run `npm run build` to confirm the error is gone.
The “fix” to avoid
Agents often wrap the call in `<Suspense>` assuming it defers execution to request time. Without Partial Prerendering, this does not work — the component still executes during static generation and throws. Another common wrong fix is adding `use client` to the component, which only makes the error appear at build time with a different message. The real fix is matching the API to a request context.
Copy-paste fix prompt for your AI coding tool
Paste this into Cursor, Claude Code, Lovable or Bolt:
My Next.js app throws: "`headers` was called outside a request scope. Read more: https://nextjs.org/docs/messages/next-dynamic-api-wrong-context". Find where headers()/cookies() are called and identify the context (Client Component, Server Component during prerender, Middleware, Route Handler, Server Action). Check in this order: 1) Call imported into a Client Component — move it to a Server Component/Route Handler/Server Action. 2) Call during static prerendering or generateStaticParams — add export const dynamic = force-dynamic to the route (makes the whole route dynamic), OR call await connection() before the call (without PPR this also makes the route dynamic). 3) Call in Middleware — use request.headers/request.cookies instead of next/headers. 4) Route Handler/Server Action without request scope — ensure it is invoked by a request. Provide the corrected code and explain which context the call now lives in.
Questions people ask
Can I use headers() in a Client Component?
No. headers() and cookies() are server-only APIs. They cannot run in the browser. Move the logic to a Server Component, Server Action, Route Handler, or Middleware. Reading them via a Route Handler (e.g., /api/.../headers) or server action is allowed and the value can be passed down.
Does adding Suspense fix the prerendering error?
Only with Partial Prerendering (PPR) enabled. Without PPR, Suspense does not prevent the error — the component still runs during static generation. `export const dynamic = 'force-dynamic'` makes the whole route dynamic. `await connection()` (from `next/server`) also makes the route dynamic if PPR is not enabled. Use PPR + Suspense for fine-grained control, or use `force-dynamic` for a fully dynamic route.
What is the difference between force-dynamic and connection()?
`force-dynamic` makes the entire route dynamic (no static caching). `connection()` marks only the component and its children as request-time. With PPR enabled, the rest of the route stays prerendered. Without PPR, `connection()` makes the route dynamic as well. Use `force-dynamic` for a fully dynamic route, or `connection()` with PPR for fine-grained control.
Official documentation
- Next.js: Request-time API was called outside request
- Next.js: headers API reference
- Next.js: cookies API reference