N Next.js 中文
原文 ↗

updateTag

updateTag 函数的 API 参考。

更新于 2026-11-18Next.js 16.4.6

updateTag 允许你在服务器操作中按需更新特定缓存标签的缓存数据。

此函数专为 read-your-own-writes(读己之写)场景而设计:用户做出更改(例如创建一篇帖子)后,UI 会立即显示该更改,而不是显示陈旧数据。

用法

updateTag 只能在服务器操作中调用。它不能在路由处理器、客户端组件或任何其他上下文中使用。

如果你需要在路由处理器或其他上下文中使缓存标签失效,请改用 revalidateTag。

须知:

updateTag 会立即使指定标签的缓存数据过期。下一次请求会等待获取新数据,而不是从缓存中提供陈旧内容,从而确保用户立即看到自己的更改。

客户端缓存行为

当服务器操作调用 updateTag 时,无论该路由是否使用该标签,Next.js 都会重新渲染当前路由,并在操作的响应中发送更新后的 UI。未缓存的数据会重新获取,而不带该标签的服务端缓存数据则会被复用。

客户端使用操作的响应来重新渲染当前路由,并清除所有已预取的路由以及为前进/后退导航保留的路由数据,而不仅仅是使用该标签的路由。因此,符合条件的可见链接可能会再次预取。由 Activity 保留的页面会保持挂载并保留其 React 状态。当你导航回这些页面时,其服务端数据会刷新。

当其 profile 解析为 expire: 0 时,revalidateTag 在服务器操作中具有相同的效果。这涵盖 { expire: 0 }、带 expire: 0 的命名 profile,以及已弃用的单参数形式。当 expire 为非零值(例如 'max')时,它会把带标签的数据标记为陈旧,并跳过重新渲染和客户端缓存清除。操作中的其他调用(例如 refresh)仍可触发重新渲染。

参数

updateTag(tag: string): void;
  • tag:一个字符串,表示与你要更新的数据关联的缓存标签。长度不得超过 256 个字符。此值区分大小写。

必须先将标签分配给缓存数据。你可以通过两种方式做到这一点:

  • 在缓存外部 API 请求时,使用 next.tags 选项并配合 fetch:
fetch(url, { next: { tags: ['posts'] } })
  • 在带有 'use cache' 指令的缓存函数或组件内部使用 cacheTag:
import { cacheTag } from 'next/cache'

async function getData() {
  'use cache'
  cacheTag('posts')
  // ...
}

返回值

updateTag 不返回任何值。

与 revalidateTag 的区别

虽然 updateTag 和 revalidateTag 都会使缓存数据失效,但它们的用途不同:

  • updateTag:

    • 只能在服务器操作中使用
    • 下一次请求会等待新数据(不会提供陈旧内容)
    • 专为读己之写场景设计
  • revalidateTag:

    • 可用于服务器操作和路由处理器
    • 使用 profile="max"(推荐):在后台获取新数据的同时提供缓存数据(stale-while-revalidate)
    • 使用自定义配置:可配置为任意缓存生命周期配置,以支持高级用法
    • 不使用配置:等效于 updateTag 的旧版行为

示例

实现读己之写的服务器操作

TypeScript

app/actions.tsts
'use server'

import { updateTag } from 'next/cache'
import { redirect } from 'next/navigation'

export async function createPost(formData: FormData) {
  const title = formData.get('title')
  const content = formData.get('content')

  // Create the post in your database
  const post = await db.post.create({
    data: { title, content },
  })

  // Invalidate cache tags so the new post is immediately visible
  // 'posts' tag: affects any page that displays a list of posts
  updateTag('posts')
  // 'post-{id}' tag: affects the individual post detail page
  updateTag(`post-${post.id}`)

  // Redirect to the new post - user will see fresh data, not cached
  redirect(`/posts/${post.id}`)
}

JavaScript

app/actions.jsjs
'use server'

import { updateTag } from 'next/cache'
import { redirect } from 'next/navigation'

export async function createPost(formData) {
  const title = formData.get('title')
  const content = formData.get('content')

  // Create the post in your database
  const post = await db.post.create({
    data: { title, content },
  })

  // Invalidate cache tags so the new post is immediately visible
  // 'posts' tag: affects any page that displays a list of posts
  updateTag('posts')
  // 'post-{id}' tag: affects the individual post detail page
  updateTag(`post-${post.id}`)

  // Redirect to the new post - user will see fresh data, not cached
  redirect(`/posts/${post.id}`)
}

在服务器操作之外使用时会报错

app/api/posts/route.tsts
import { revalidateTag, updateTag } from 'next/cache'

export async function POST() {
  // This will throw an error
  updateTag('posts')
  // Error: updateTag can only be called from within a Server Action

  // Use revalidateTag instead in Route Handlers
  revalidateTag('posts', 'max')
}

何时使用 updateTag

在以下情况下使用 updateTag:

  • 你处于服务器操作中
  • 你需要为读己之写立即执行缓存失效
  • 你希望确保下一次请求能看到更新后的数据

在以下情况下改用 revalidateTag:

  • 你处于路由处理器或其他非操作上下文中
  • 你需要 stale-while-revalidate 语义
  • 你正在构建用于缓存失效的 webhook 或 API 端点

相关内容