use cache: private
Learn how to use the `"use cache: private"` directive to enable runtime prefetching of personalized content in your Next.js application.
The 'use cache: private' directive works just like use cache, but allows you to use runtime APIs like cookies, headers, or search params.
Good to know: Unlike use cache, private caches are not prerendered statically as they contain personalized data that is not shared between users.
Usage
To use 'use cache: private', enable the cacheComponents flag in your next.config.ts file:
Then add 'use cache: private' to your function along with a cacheLife configuration.
Basic example
Request APIs allowed in private caches
The following request-specific APIs can be used inside 'use cache: private' functions:
| API | Allowed in use cache | Allowed in 'use cache: private' |
|---|---|---|
cookies() | No | Yes |
headers() | No | Yes |
searchParams | No | Yes |
connection() | No | No |
Note: The connection() API is prohibited in both use cache and 'use cache: private' as it provides connection-specific information that cannot be safely cached.
Version History
| Version | Changes |
|---|---|
v16.0.0 | "use cache: private" is enabled with the Cache Components feature. |