Skip to content

fix: use next/root-params instead of headers() in cache components docs#304

Merged
moss-bryophyta merged 2 commits into
mainfrom
fix/cache-components-root-params
May 19, 2026
Merged

fix: use next/root-params instead of headers() in cache components docs#304
moss-bryophyta merged 2 commits into
mainfrom
fix/cache-components-root-params

Conversation

@moss-bryophyta
Copy link
Copy Markdown
Collaborator

Problem

The cache components docs recommended using headers() in getLocale.ts / getRegion.ts. This breaks inside "use cache" boundaries because Next.js forbids dynamic APIs like headers() in cached scopes:

Error: Route /[locale] used `headers()` inside "use cache".
Accessing Dynamic data sources inside a cache scope is not supported.

Solution

Replace headers() with the next/root-params API (Next.js 16.2+). Root params can be read from anywhere in the component tree — including inside "use cache" boundaries — and the root param value automatically becomes part of the cache key.

Requires:

  • experimental.rootParams: true in next.config.js
  • [locale] to be a root parameter (no app/layout.tsx above it)

Context

Follow-up to #303. Identified via testing — see thread in #moss-pager.

The previous getLocale/getRegion examples used headers() which breaks
inside "use cache" boundaries. Replace with next/root-params API
(Next.js 16.2+) which is cache-safe and automatically becomes part
of the cache key.
@moss-bryophyta moss-bryophyta requested a review from a team as a code owner May 19, 2026 18:16
@moss-bryophyta moss-bryophyta enabled auto-merge (squash) May 19, 2026 18:16
- rootParams doesn't need a config flag, it's handled by the compiler
- getLocale just returns await locale() directly
- getRegion returns undefined instead of empty string
@moss-bryophyta moss-bryophyta merged commit 91ce0df into main May 19, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants