Overview
ThecreateSearchParamsCache function creates a cache interface for accessing search parameters in deeply nested server components. It uses React’s cache function to ensure parsed values are available throughout the server component tree for a single request.
The cache only works in server components. For client components, use
useQueryState or useQueryStates hooks.Basic Usage
Function Signature
Parameters
ParserMap
required
An object mapping search param keys to their parser configurations.
object
Optional configuration object.
Cache Interface Methods
The returned cache interface provides three methods:parse()
Parses the incoming search params and stores them in the cache for the current request.SearchParams | Promise<SearchParams>
required
The
searchParams prop from your page component. In Next.js 15+, this may be a Promise.LoaderFunctionOptions
get()
Retrieves a single cached search param value by key.all()
Retrieves all cached search param values as an object.Next.js 15 Support (Async searchParams)
Next.js 15 introduced a breaking change where thesearchParams prop became a Promise. The cache handles both synchronous and asynchronous search params:
Complete Example
1
Define the cache
Create a shared cache configuration file:
2
Parse in the page component
Call
parse() in your top-level page component:3
Access in nested components
Use
get() or all() in any nested server component:Cache Lifecycle
The cache is scoped to a single page render using React’scache function:
- Created when the page component renders
- Shared across all server components in the tree for that request
- Cleared after the request completes
- Isolated between different requests (no cross-request pollution)
Strict Mode
Use strict mode to validate search params and throw errors for invalid values:URL Keys Mapping
Map internal keys to different URL parameter names:Sharing with Client Components
You can share parser definitions between server and client code:Error Handling
The cache will throw an error if you try to access values before callingparse():
parse() multiple times with different inputs:
Type Inference
TypeScript infers the correct types based on your parsers:Best Practices
1
Always call parse() first
Call
parse() in your page component before any nested components try to access the cache.2
Use withDefault for required values
Avoid null checks by providing default values:
3
Create a single cache per feature
Define one cache configuration per page or feature area and reuse it across all related components.
4
Share parsers with client components
Export your parser configuration separately so both server cache and client hooks can use the same definitions.
Related
Loaders
Use createLoader for one-off parsing
Parsers
Learn about available parser types