You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/01-app/03-api-reference/01-directives/use-cache-private.mdx
+31-7Lines changed: 31 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,21 +1,31 @@
1
1
---
2
2
title: 'use cache: private'
3
-
description: 'Learn how to use the `"use cache: private"` directive to enable runtime prefetching of personalized content in your Next.js application.'
3
+
description: 'Learn how to use the "use cache: private" directive to cache functions that access runtime request APIs.'
The `'use cache: private'` directive works just like [`use cache`](/docs/app/api-reference/directives/use-cache), but allows you to use runtime APIs like cookies, headers, or search params.
15
+
The `'use cache: private'` directive allows functions to access runtime request APIs like `cookies()`, `headers()`, and `searchParams` within a cached scope. However, results are **never stored on the server**, they're cached only in the browser's memory and do not persist across page reloads.
17
16
18
-
> **Good to know:** Unlike `use cache`, private caches are not prerendered statically as they contain personalized data that is not shared between users.
17
+
Reach for `'use cache: private'` when:
18
+
19
+
- You want to cache a function that already accesses runtime data, and refactoring to [move the runtime access outside and pass values as arguments](/docs/app/getting-started/cache-components#with-runtime-data) is not practical.
20
+
- Compliance requirements prevent storing certain data on the server, even temporarily
21
+
22
+
Because this directive accesses runtime data, the function executes on every server render and is excluded from running during [static shell](/docs/app/getting-started/cache-components#how-rendering-works-with-cache-components) generation.
23
+
24
+
It is **not** possible to configure custom cache handlers for `'use cache: private'`.
25
+
26
+
For a comparison of the different cache directives, see [How `use cache: remote` differs from `use cache` and `use cache: private`](/docs/app/api-reference/directives/use-cache-remote#how-use-cache-remote-differs-from-use-cache-and-use-cache-private).
27
+
28
+
> **Good to know**: This directive is marked as `experimental` because it depends on runtime prefetching, which is not yet stable. Runtime prefetching is an upcoming feature that will let the router prefetch past the [static shell](/docs/app/getting-started/cache-components#how-rendering-works-with-cache-components) into **any** cached scope, not just private caches.
19
29
20
30
## Usage
21
31
@@ -42,13 +52,21 @@ export default nextConfig
42
52
43
53
Then add `'use cache: private'` to your function along with a `cacheLife` configuration.
44
54
55
+
> **Good to know**: This directive is not available in Route Handlers.
56
+
45
57
### Basic example
46
58
59
+
In this example, we demonstrate that you can access cookies within a `'use cache: private'` scope:
@@ -132,6 +154,8 @@ async function getRecommendations(productId) {
132
154
}
133
155
```
134
156
157
+
> **Good to know**: The `stale` time must be at least 30 seconds for runtime prefetching to work. See [`cacheLife` client router cache behavior](/docs/app/api-reference/functions/cacheLife#client-router-cache-behavior) for details.
158
+
135
159
## Request APIs allowed in private caches
136
160
137
161
The following request-specific APIs can be used inside `'use cache: private'` functions:
0 commit comments