Files
vercel__next.js/errors/next-dynamic-api-wrong-context.mdx
Hendrik Liebau 4f69336336 Support accessing root params in generateStaticParams (#91189)
A new `GenerateStaticParamsStore` work unit store type is now provided during `generateStaticParams` execution. This enables root param getters (`import { lang } from 'next/root-params'`) to be called inside `generateStaticParams`, allowing shared helpers that internally access root params via the special import to be used in both Server Components and `generateStaticParams` without manually threading params.

Each `generateStaticParams` call now runs within a `workUnitAsyncStorage.run()` context carrying a `GenerateStaticParamsStore` with the correct `rootParams` (extracted from `parentParams` using the already-available `rootParamKeys`). The store extends `CommonWorkUnitStore`, providing `phase` and `implicitTags` which are not strictly necessary but convenient to keep call sites simple.

Request-time APIs (`headers()`, `cookies()`, `connection()`, `draftMode()`) now throw specific errors when called inside `generateStaticParams` instead of the previous generic "called outside a request scope" message. Framework-internal functions like `createSearchParamsFromClient` and `createParamsFromClient` throw `InvariantError` since they should never be reached in this context.

This change also unblocks a follow-up PR that removes `| undefined` from `PublicCacheContext.outerWorkUnitStore` in the use cache wrapper, since `"use cache"` is already supported inside `generateStaticParams` today but previously ran without a `WorkUnitStore`. With this store in place, requiring a `WorkUnitStore` in `"use cache"` won't break that existing usage.
2026-03-16 15:44:53 +01:00

50 lines
1.6 KiB
Plaintext

---
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:
```diff filename="app/page.js"
import { cookies } from 'next/headers'
- const cookieStore = await cookies()
export default async function Page() {
+ const cookieStore = await cookies()
return ...
}
```
```diff filename="app/foo/route.js"
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.
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.
## Useful Links
- [`headers()` function](/docs/app/api-reference/functions/headers)
- [`cookies()` function](/docs/app/api-reference/functions/cookies)
- [`draftMode()` function](/docs/app/api-reference/functions/draft-mode)
- [`unstable_noStore()` function](/docs/app/api-reference/functions/unstable_noStore)
- [`unstable_cache()` function](/docs/app/api-reference/functions/unstable_cache)