重新验证
了解如何使用基于时间和按需的策略重新验证缓存数据。
本页介绍使用 Cache Components 进行重新验证,只需在
next.config.ts文件中设置cacheComponents: true即可启用。如果你未使用 Cache Components,请参阅 缓存与重新验证(先前模型) 指南。
重新验证是更新缓存数据的过程。它让你能够继续提供快速的缓存响应,同时确保内容保持最新。共有两种策略:
- 基于时间的重新验证:使用
cacheLife在设定的时长过后自动刷新缓存数据。 - 按需重新验证:在发生变更后,使用
revalidateTag、updateTag或revalidatePath手动使缓存数据失效。
cacheLife
cacheLife 控制缓存数据的有效时长。在 use cache 作用域内使用它来设置缓存寿命。
import { cacheLife } from 'next/cache'
export async function getProducts() {
'use cache'
cacheLife('hours')
return db.query('SELECT * FROM products')
}
cacheLife 接受 profile 名称或自定义配置对象:
| Profile | stale |
revalidate |
expire |
|---|---|---|---|
default |
5m | 15m | never |
seconds |
30s | 1s | 60s |
minutes |
5m | 1m | 1h |
hours |
5m | 1h | 1d |
days |
5m | 1d | 1w |
weeks |
5m | 1w | 30d |
max |
5m | 30d | 1y |
如需更精细的控制,可以传入一个对象:
'use cache'
cacheLife({
stale: 3600, // 1 hour until considered stale
revalidate: 7200, // 2 hours until revalidated
expire: 86400, // 1 day until expired
})
须知:
当缓存使用
secondsprofile、revalidate: 0,或时长低于 5 分钟的expire时,该缓存被视为“短生命周期”。短生命周期缓存会自动从预渲染中排除,转而变成动态空洞。详情请参阅 预渲染行为。
有关所有 profile 和自定义配置选项,请参阅 cacheLife API 参考。
cacheTag
cacheTag 让你为缓存数据打上标签,以便按需使其失效。在 use cache 作用域内使用它:
TypeScript
import { cacheTag } from 'next/cache'
export async function getProducts() {
'use cache'
cacheTag('products')
return db.query('SELECT * FROM products')
}
JavaScript
import { cacheTag } from 'next/cache'
export async function getProducts() {
'use cache'
cacheTag('products')
return db.query('SELECT * FROM products')
}
打上标签后,可使用 revalidateTag 或 updateTag 使缓存失效。
请参阅 cacheTag API 参考 了解更多。
revalidateTag
使用推荐的 'max' profile 的 revalidateTag 会基于 stale-while-revalidate 语义按标签使缓存条目失效。旧内容会立即返回,同时新内容在后台加载。这非常适合更新稍有延迟也可接受的内容,例如博客文章或产品目录。
TypeScript
import { revalidateTag } from 'next/cache'
export async function updateUser(id: string) {
// Mutate data
revalidateTag('user', 'max') // Recommended: stale-while-revalidate
}
JavaScript
import { revalidateTag } from 'next/cache'
export async function updateUser(id) {
// Mutate data
revalidateTag('user', 'max') // Recommended: stale-while-revalidate
}
你可以在多个函数中复用同一个标签,从而一次性重新验证它们全部。在 服务器操作 或 路由处理器 中调用 revalidateTag。
须知:
第二个参数设定在后台生成新内容期间可以继续提供陈旧内容的时长。一旦过期,后续请求将被阻塞,直到新内容准备就绪。使用
'max'可获得最长的陈旧内容窗口。
请参阅 revalidateTag API 参考 了解更多。
updateTag
updateTag 会立即让缓存数据过期,适用于读己之所写(read-your-own-writes)场景——用户能立刻看到自己的更改,而不是陈旧内容。与 revalidateTag 不同,它只能在服务器操作中使用。
须知:
updateTag还会刷新调用方的路由,并使客户端缓存条目失效。有关作用范围以及与revalidateTag的对比,请参阅客户端缓存行为。
TypeScript
import { updateTag } from 'next/cache'
import { redirect } from 'next/navigation'
export async function createPost(formData: FormData) {
const post = await db.post.create({
data: {
title: formData.get('title'),
content: formData.get('content'),
},
})
updateTag('posts')
redirect(`/posts/${post.id}`)
}
JavaScript
import { updateTag } from 'next/cache'
import { redirect } from 'next/navigation'
export async function createPost(formData) {
const post = await db.post.create({
data: {
title: formData.get('title'),
content: formData.get('content'),
},
})
updateTag('posts')
redirect(`/posts/${post.id}`)
}
updateTag |
revalidateTag(tag, 'max') |
|
|---|---|---|
| 可用位置 | 仅限服务器操作 | 服务器操作和路由处理器 |
| 行为 | 立即让缓存过期 | 陈旧内容先返回并重新验证(stale-while-revalidate) |
| 使用场景 | 读己之所写(用户能看到自己的更改) | 后台刷新(稍有延迟无妨) |
请参阅 updateTag API 参考 了解更多。
revalidatePath
revalidatePath 会使特定路由路径的所有缓存数据失效。当你想重新验证某个路由但并不清楚它关联了哪些标签时,可以使用它。
TypeScript
import { revalidatePath } from 'next/cache'
export async function updateUser(id: string) {
// Mutate data
revalidatePath('/profile')
}
JavaScript
import { revalidatePath } from 'next/cache'
export async function updateUser(id) {
// Mutate data
revalidatePath('/profile')
}
须知:
尽可能优先使用基于标签的重新验证(
revalidateTag/updateTag),而不是基于路径的方式。标签能更精确地定位服务器端缓存数据,避免过度失效。客户端缓存失效的作用范围更广。
请参阅 revalidatePath API 参考 了解更多。
我应该缓存什么?
缓存不依赖运行时数据的数据,并且你可以在一定时间内接受从缓存中提供这些数据。使用 use cache 配合 cacheLife 来描述这一行为。
当内容不需要基于时间的重新验证时,例如来自 CMS 的数据,请使用 cacheTag 和较长的 cacheLife(如 max),将其保留在静态外壳中。配置内容源以触发 webhook 或其他通知,在内容发生变化时调用 revalidateTag。这样可以减少对未变化内容进行不必要的基于时间的重新验证。
须知:
在无服务器环境中,内存缓存条目可能无法在多次重新验证之间持久保留。详情请参阅运行时缓存注意事项。
API 参考
进一步了解本页提到的 API。
- cacheLife:了解如何使用 cacheLife 函数为已缓存的函数或组件设置缓存过期时间。
- cacheTag:了解如何使用 cacheTag 函数在 Next.js 应用中管理缓存失效。
- revalidateTag:revalidateTag 函数的 API 参考。
- updateTag:updateTag 函数的 API 参考。
- revalidatePath:revalidatePath 函数的 API 参考。