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
description: Learn more about the features mentioned in this page by reading the API Reference.
7
7
links:
8
8
- app/api-reference/functions/fetch
9
-
- app/api-reference/functions/unstable_cache
10
-
- app/api-reference/functions/revalidatePath
9
+
- app/api-reference/functions/cacheTag
11
10
- app/api-reference/functions/revalidateTag
12
11
- app/api-reference/functions/updateTag
12
+
- app/api-reference/functions/revalidatePath
13
+
- app/api-reference/functions/unstable_cache
13
14
---
14
15
15
16
Caching is a technique for storing the result of data fetching and other computations so that future requests for the same data can be served faster, without doing the work again. While revalidation allows you to update cache entries without having to rebuild your entire application.
16
17
17
18
Next.js provides a few APIs to handle caching and revalidation. This guide will walk you through when and how to use them.
18
19
19
20
-[`fetch`](#fetch)
20
-
-[`unstable_cache`](#unstable_cache)
21
-
-[`revalidatePath`](#revalidatepath)
21
+
-[`cacheTag`](#cachetag)
22
22
-[`revalidateTag`](#revalidatetag)
23
23
-[`updateTag`](#updatetag)
24
+
-[`revalidatePath`](#revalidatepath)
25
+
-[`unstable_cache`](#unstable_cache) (Legacy)
24
26
25
27
## `fetch`
26
28
@@ -56,103 +58,65 @@ export default async function Page() {
56
58
57
59
This will revalidate the data after a specified amount of seconds.
58
60
59
-
See the [`fetch` API reference](/docs/app/api-reference/functions/fetch) to learn more.
60
-
61
-
## `unstable_cache`
61
+
You can also tag `fetch` requests to enable on-demand cache invalidation:
62
62
63
-
`unstable_cache` allows you to cache the result of database queries and other async functions. To use it, wrap `unstable_cache` around the function. For example:
See the [`fetch` API reference](/docs/app/api-reference/functions/fetch) to learn more.
91
84
92
-
exportdefaultasyncfunction Page({
93
-
params,
94
-
}: {
95
-
params:Promise<{ userId:string }>
96
-
}) {
97
-
const { userId } =awaitparams
85
+
## `cacheTag`
98
86
99
-
const getCachedUser =unstable_cache(
100
-
async () => {
101
-
returngetUserById(userId)
102
-
},
103
-
[userId] // add the user ID to the cache key
104
-
)
105
-
}
106
-
```
87
+
[`cacheTag`](/docs/app/api-reference/functions/cacheTag) allows you to tag cached data in [Cache Components](/docs/app/getting-started/cache-components) so it can be revalidated on-demand. Previously, cache tagging was limited to `fetch` requests, and caching other work required the experimental `unstable_cache` API.
With Cache Components, you can use the [`use cache`](/docs/app/api-reference/directives/use-cache) directive to cache any computation, and `cacheTag` to tag it. This works with database queries, file system operations, and other server-side work.
111
90
112
-
exportdefaultasyncfunctionPage({ params }) {
113
-
const { userId } =await params
91
+
```tsx filename="app/lib/data.ts" switcher
92
+
import { cacheTag } from'next/cache'
114
93
115
-
constgetCachedUser=unstable_cache(
116
-
async () => {
117
-
returngetUserById(userId)
118
-
},
119
-
[userId] // add the user ID to the cache key
120
-
)
94
+
exportasyncfunction getProducts() {
95
+
'use cache'
96
+
cacheTag('products')
97
+
98
+
const products =awaitdb.query('SELECT * FROM products')
99
+
returnproducts
121
100
}
122
101
```
123
102
124
-
The function accepts a third optional object to define how the cache should be revalidated. It accepts:
103
+
```jsx filename="app/lib/data.js" switcher
104
+
import { cacheTag } from'next/cache'
125
105
126
-
-`tags`: an array of tags used by Next.js to revalidate the cache.
127
-
-`revalidate`: the number of seconds after cache should be revalidated.
Once tagged, you can use [`revalidateTag`](#revalidatetag) or [`updateTag`](#updatetag) to invalidate the cache entry for products.
154
116
155
-
See the [`unstable_cache` API reference](/docs/app/api-reference/functions/unstable_cache) to learn more.
117
+
> **Good to know**: `cacheTag` is used with [Cache Components](/docs/app/getting-started/cache-components) and the [`use cache`](/docs/app/api-reference/directives/use-cache) directive. It expands the caching and revalidation story beyond `fetch`.
118
+
119
+
See the [`cacheTag` API reference](/docs/app/api-reference/functions/cacheTag) to learn more.
156
120
157
121
## `revalidateTag`
158
122
@@ -161,55 +125,7 @@ See the [`unstable_cache` API reference](/docs/app/api-reference/functions/unsta
161
125
-**With `profile="max"`**: Uses stale-while-revalidate semantics, serving stale content while fetching fresh content in the background
162
126
-**Without the second argument**: Legacy behavior that immediately expires the cache (deprecated)
163
127
164
-
To use it with `fetch`, start by tagging the function with the `next.tags` option:
['user'], // Needed if variables are not passed as parameters
206
-
{
207
-
tags: ['user'],
208
-
}
209
-
)
210
-
```
211
-
212
-
Then, call `revalidateTag` in a [Route Handler](/docs/app/api-reference/file-conventions/route) or Server Action:
128
+
After tagging your cached data, using [`fetch`](#fetch) with `next.tags`, or the [`cacheTag`](#cachetag) function, you may call `revalidateTag` in a [Route Handler](/docs/app/api-reference/file-conventions/route) or Server Action:
@@ -233,28 +149,6 @@ You can reuse the same tag in multiple functions to revalidate them all at once.
233
149
234
150
See the [`revalidateTag` API reference](/docs/app/api-reference/functions/revalidateTag) to learn more.
235
151
236
-
## `revalidatePath`
237
-
238
-
`revalidatePath` is used to revalidate a route and following an event. To use it, call it in a [Route Handler](/docs/app/api-reference/file-conventions/route) or Server Action:
See the [`revalidatePath` API reference](/docs/app/api-reference/functions/revalidatePath) to learn more.
257
-
258
152
## `updateTag`
259
153
260
154
`updateTag` is specifically designed for Server Actions to immediately expire cached data for read-your-own-writes scenarios. Unlike `revalidateTag`, it can only be used within Server Actions and immediately expires the cache entry.
@@ -307,3 +201,123 @@ The key differences between `revalidateTag` and `updateTag`:
307
201
-**`revalidateTag`**: In Server Actions and Route Handlers, supports stale-while-revalidate with `profile="max"`
308
202
309
203
See the [`updateTag` API reference](/docs/app/api-reference/functions/updateTag) to learn more.
204
+
205
+
## `revalidatePath`
206
+
207
+
`revalidatePath` is used to revalidate a route and following an event. To use it, call it in a [Route Handler](/docs/app/api-reference/file-conventions/route) or Server Action:
See the [`revalidatePath` API reference](/docs/app/api-reference/functions/revalidatePath) to learn more.
226
+
227
+
## `unstable_cache`
228
+
229
+
> **Good to know**: `unstable_cache` is an experimental API. We recommend opting into [Cache Components](/docs/app/getting-started/cache-components) and replacing `unstable_cache` with the [`usecache`](/docs/app/api-reference/directives/use-cache) directive. See the [Cache Components documentation](/docs/app/getting-started/cache-components) for more details.
230
+
231
+
`unstable_cache` allows you to cache the result of database queries and other async functions. To use it, wrap `unstable_cache` around the function. For example:
0 commit comments