next/root-params
API Reference for the next/root-params module that provides access to root-level route parameters.
The next/root-params module provides getter functions for accessing root-level parameters in Server Components. Each root parameter is exported as an async function that resolves to the parameter value for the current route.
The export names are generated from your dynamic segment folder names. For example, if your root layout is inside app/[locale], you import locale from next/root-params.
This is useful for values like a language or locale segment that need to be accessed across your application, for example in shared utilities, layouts, and deeply nested components, without passing them down as props.
Root parameters are the dynamic segments that appear before the root layout. Unlike the regular params prop, root parameter getters can be called from any Server Component in your application without prop drilling. See Why root parameters are special for more details.
Good to know:
- Root parameter names must be valid JavaScript function identifiers. Kebab-cased segment names (e.g.
[post-slug]) are not supported and will cause an error at dev time or during build. next/root-paramscan be used in Server Components. It cannot be used in Client Components, Server Actions, or Route Handlers. Support for Route Handlers is planned for a future release.- With Cache Components enabled,
generateStaticParamsmust return at least one value for each root parameter. Providing values makes them static route parameters, required for prerendering static shells. - The examples on this page use
PagePropsandLayoutProps, which are auto-generated type helpers based on your route structure.
Root parameters and other route parameters
Root parameters are the route parameters from dynamic segments that appear in the path up to the root layout file. Other route parameters deeper in the route tree are accessed through the params prop.
Given this file structure:
app/[lang]/layout.tsx(root layout)app/[lang]/posts/[slug]/page.tsx
Only lang is a root parameter. slug is a regular route parameter:
Accessing root parameters in shared code
Root parameter getters are module imports, they can be called from any Server Component or server-side utility, not just layouts and pages:
Good to know: You do not need to add import 'server-only' to files that use next/root-params. The import already fails at build time if used in a Client Component.
Behavior with caching directives
Because root parameter getters are imported functions, Next.js can track which ones a cached function uses. Only those root parameters become part of the cache key, so cache entries are not split across unrelated parameter values.
With the regular params prop, you would need to await params outside the cached function and pass the value in as an argument:
Use in generateStaticParams for nested segments
Inside a nested segment's generateStaticParams, you can read a root parameter directly with its getter, instead of destructuring it from the params argument:
Multiple root layouts
When an application has multiple root layouts with different parameters, getter functions are typed to account for usage in any of all possible routes. A parameter that does not exist in every root layout has the type string | undefined.
Given this file structure:
app/dashboard/[id]/layout.tsx(root layout withid)app/marketing/layout.tsx(root layout withoutid)
Since id does not exist in the marketing root layout, await id() returns undefined when accessed from marketing routes.
Catch-all and optional catch-all segments
Root parameters work with catch-all and optional catch-all segments:
Returns
Each root parameter getter returns a Promise that resolves to one of the following:
| Segment type | Example | Return type |
|---|---|---|
| Dynamic | [id] | string |
| Catch-all | [...path] | string[] |
| Optional catch-all | [[...path]] | string[] | undefined |
If the parameter does not exist in the current route's root layout (see Multiple root layouts), the return type includes undefined.
Restrictions
Most of these are permanent constraints of how root parameters work. The exception is Route Handlers, which are not supported yet but are planned for a future release.
Server Components only
Root parameter getters can only be used in Server Components:
Not available in unstable_cache
Calling a root parameter getter inside unstable_cache will throw a runtime error. Use "use cache" instead.
Not available in Server Actions
Why root parameters are special
The root layout is the top-level rendering boundary. The route parameters before it are shared by all routes under that root layout, which is what makes them safe to access from any Server Component in that tree. Route parameters deeper in the route vary depending on which child page is being rendered, so they are only available through the params prop in the page or layout that defines them.
In this example, lang is the only root parameter. The page.tsx directly under the root layout has no slug at all. The blog and store branches both define slug, but with different segment types ([slug] vs [...slug]). Each route only knows about its own route parameters. The same name can have different types, different meanings, or not exist at all depending on the route. This is why route parameters below the root layout are accessed through the params prop rather than as global getters.
The exception is multiple root layouts. When different root layouts define different route parameters, the getter functions include undefined in their return type to account for routes where a given parameter may not exist.
Version History
| Version | Changes |
|---|---|
v16.3.0 | next/root-params introduced. |