NNext.js 中文网 GitHub

cacheTag

了解如何使用 cacheTag 函数管理 Next.js 应用程序中的缓存失效。

cacheTag 函数允许你为缓存数据添加标签,以便按需失效。通过将标签与缓存条目关联,你可以有选择地清除或重新验证特定的缓存条目,而不影响其他缓存数据。

用法#

要使用 cacheTag,请在 next.config.js 文件中启用 cacheComponents 标志:

next.config.ts
import type { NextConfig } from 'next'

const nextConfig: NextConfig = {
  cacheComponents: true,
}

export default nextConfig
next.config.js
const nextConfig = {
  cacheComponents: true,
}

module.exports = nextConfig

cacheTag 函数接受一个或多个字符串值。

app/data.ts
import { cacheTag } from 'next/cache'

export async function getData() {
  'use cache'
  cacheTag('my-data')
  const res = await fetch('https://api.example.com/data')
  const data = await res.json()
  return data
}
app/data.js
import { cacheTag } from 'next/cache'

export async function getData() {
  'use cache'
  cacheTag('my-data')
  const res = await fetch('https://api.example.com/data')
  const data = await res.json()
  return data
}

然后,你可以从 Server Function 或 Route Handler 按需清除缓存:

  • 使用 updateTag 适用于"读己所写"的场景,例如表单和其他由用户触发的变更:用户做出更改后,下一次读取应立即获取新数据。updateTag 只能在 Server Function 内部使用。
  • 当可以接受在后台重新验证期间提供陈旧数据时,或者当从 Route Handler 或其他上下文进行重新验证时,使用 revalidateTag。

例如,这个 Server Function 添加一篇帖子,然后清除所有带有 'my-data' 标签的缓存条目,使下一次读取反映该更改:

app/action.ts
'use server'

import { updateTag } from 'next/cache'

export default async function submit() {
  await addPost()
  updateTag('my-data')
}
app/action.js
'use server'

import { updateTag } from 'next/cache'

export default async function submit() {
  await addPost()
  updateTag('my-data')
}

对于有多个服务器实例的部署,请参阅分布式标签协调。

小知识#

  • 幂等标签:多次应用同一个标签不会产生额外效果。
  • 多个标签:你可以通过向 cacheTag 传递多个字符串值,为单个缓存条目分配多个标签。
tsx
cacheTag('tag-one', 'tag-two')
  • 限制:单次 cacheTag() 调用最多接受 128 个标签,每个标签的最大长度为 256 个字符。超过 256 个字符的标签会被跳过,一次调用中第 128 个之后的标签会被丢弃。这两种情况都会在控制台记录警告。

示例#

为组件或函数添加标签#

在缓存函数或组件中调用 cacheTag,为缓存数据添加标签:

app/components/bookings.tsx
import { cacheTag } from 'next/cache'

interface BookingsProps {
  type: string
}

export async function Bookings({ type = 'haircut' }: BookingsProps) {
  'use cache'
  cacheTag('bookings-data')

  async function getBookingsData() {
    const response = await fetch(
      `https://api.example.com/bookings?type=${encodeURIComponent(type)}`
    )
    const data = await response.json()
    return data
  }

  return //...
}
app/components/bookings.js
import { cacheTag } from 'next/cache'

export async function Bookings({ type = 'haircut' }) {
  'use cache'
  cacheTag('bookings-data')

  async function getBookingsData() {
    const response = await fetch(
      `https://api.example.com/bookings?type=${encodeURIComponent(type)}`
    )
    const data = await response.json()
    return data
  }

  return //...
}

基于外部数据创建标签#

你可以使用异步函数返回的数据为缓存条目添加标签。

app/components/bookings.tsx
import { cacheTag } from 'next/cache'

interface BookingsProps {
  type: string
}

export async function Bookings({ type = 'haircut' }: BookingsProps) {
  async function getBookingsData() {
    'use cache'
    const response = await fetch(
      `https://api.example.com/bookings?type=${encodeURIComponent(type)}`
    )
    const data = await response.json()
    cacheTag('bookings-data', data.id)
    return data
  }
  return //...
}
app/components/bookings.js
import { cacheTag } from 'next/cache'

export async function Bookings({ type = 'haircut' }) {
  async function getBookingsData() {
    'use cache'
    const response = await fetch(
      `https://api.example.com/bookings?type=${encodeURIComponent(type)}`
    )
    const data = await response.json()
    cacheTag('bookings-data', data.id)
    return data
  }
  return //...
}

使带标签的缓存失效#

使用 revalidateTag,你可以在需要时使特定标签的缓存失效:

app/actions.ts
'use server'

import { revalidateTag } from 'next/cache'

export async function updateBookings() {
  await updateBookingData()
  revalidateTag('bookings-data', 'max')
}
app/actions.js
'use server'

import { revalidateTag } from 'next/cache'

export async function updateBookings() {
  await updateBookingData()
  revalidateTag('bookings-data', 'max')
}

版本历史#

版本变更
v16.0.0cacheTag 已稳定。unstable_cacheTag 已弃用。

请参阅 Next.js 16 升级指南,将 unstable_cacheTag 的导入更新为 cacheTag。

Next.js 中文网