Skip to content

Commit b1b5934

Browse files
authored
docs: use cache private redo (#87111)
1 parent f6df65c commit b1b5934

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

docs/01-app/03-api-reference/01-directives/use-cache-private.mdx

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
11
---
22
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.'
4+
version: experimental
45
related:
56
title: Related
67
description: View related API references.
78
links:
89
- app/api-reference/directives/use-cache
910
- app/api-reference/config/next-config-js/cacheComponents
10-
- app/api-reference/config/next-config-js/cacheHandlers
1111
- app/api-reference/functions/cacheLife
1212
- app/api-reference/functions/cacheTag
13-
- app/guides/prefetching
1413
---
1514

16-
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.
1716

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.
1929
2030
## Usage
2131

@@ -42,13 +52,21 @@ export default nextConfig
4252

4353
Then add `'use cache: private'` to your function along with a `cacheLife` configuration.
4454

55+
> **Good to know**: This directive is not available in Route Handlers.
56+
4557
### Basic example
4658

59+
In this example, we demonstrate that you can access cookies within a `'use cache: private'` scope:
60+
4761
```tsx filename="app/product/[id]/page.tsx" switcher
4862
import { Suspense } from 'react'
4963
import { cookies } from 'next/headers'
5064
import { cacheLife, cacheTag } from 'next/cache'
5165

66+
export async function generateStaticParams() {
67+
return [{ id: '1' }]
68+
}
69+
5270
export default async function ProductPage({
5371
params,
5472
}: {
@@ -81,7 +99,7 @@ async function Recommendations({ productId }: { productId: string }) {
8199
async function getRecommendations(productId: string) {
82100
'use cache: private'
83101
cacheTag(`recommendations-${productId}`)
84-
cacheLife({ stale: 60 }) // Minimum 30 seconds required for runtime prefetch
102+
cacheLife({ stale: 60 })
85103

86104
// Access cookies within private cache functions
87105
const sessionId = (await cookies()).get('session-id')?.value || 'guest'
@@ -95,6 +113,10 @@ import { Suspense } from 'react'
95113
import { cookies } from 'next/headers'
96114
import { cacheLife, cacheTag } from 'next/cache'
97115

116+
export async function generateStaticParams() {
117+
return [{ id: '1' }]
118+
}
119+
98120
export default async function ProductPage({ params }) {
99121
const { id } = await params
100122

@@ -123,7 +145,7 @@ async function Recommendations({ productId }) {
123145
async function getRecommendations(productId) {
124146
'use cache: private'
125147
cacheTag(`recommendations-${productId}`)
126-
cacheLife({ stale: 60 }) // Minimum 30 seconds required for runtime prefetch
148+
cacheLife({ stale: 60 })
127149

128150
// Access cookies within private cache functions
129151
const sessionId = (await cookies()).get('session-id')?.value || 'guest'
@@ -132,6 +154,8 @@ async function getRecommendations(productId) {
132154
}
133155
```
134156
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+
135159
## Request APIs allowed in private caches
136160
137161
The following request-specific APIs can be used inside `'use cache: private'` functions:

docs/01-app/03-api-reference/01-directives/use-cache.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: use cache
3-
description: Learn how to use the use cache directive to cache data in your Next.js application.
3+
description: Learn how to use the "use cache" directive to cache data in your Next.js application.
44
related:
55
title: Related
66
description: View related API references.

0 commit comments

Comments
 (0)