next-dynamic-api-wrong-context.mdx


title: Request-time API was called outside request

Why This Error Occurred

A Request-time API was called outside a request scope. (Eg.: Global scope).

Note that Request-time APIs could have been called deep inside other modules/functions (eg.: third-party libraries) that are not immediately visible.

Possible Ways to Fix It

Make sure that all Request-time API calls happen in a request scope.

Example:

import { cookies } from 'next/headers'

- const cookieStore = await cookies()
export default async function Page() {
+ const cookieStore = await cookies()
  return ...
}
import { headers } from 'next/headers'

- const headersList = await headers()
export async function GET() {
+ const headersList = await headers()
  return ...
}

generateStaticParams

Request-time APIs like headers(), cookies(), connection(), and draftMode() are not available inside generateStaticParams because it runs at build time to determine which pages to statically generate. There is no HTTP request in this context.

Likewise, prefetch() and navigation() are not available there because no prefetch or navigation takes place while Next.js generates route parameters.

Note: Root param getters (import { lang } from 'next/root-params') are supported inside generateStaticParams for nested segments, as the parent params are known at that point.

Rendered from the public repository vercel/next.js on GitHub; content belongs to its authors. Cached for up to an hour.

errors/next-dynamic-api-wrong-context.mdx · vercel/next.js | Docs MD