跳到主要内容

unstable_expireTag

unstable_expireTag 允许你按需清除 缓存数据 以获取特定缓存标记。

¥unstable_expireTag allows you to purge cached data on-demand for a specific cache tag.

很高兴知道:

¥Good to know:

  • unstable_expireTagNode.js 和 Edge 运行时 中均可用。

    ¥unstable_expireTag is available in both Node.js and Edge runtimes.

  • unstable_expireTag 仅在下次访问该路径时使缓存无效。这意味着使用动态路由段调用 unstable_expireTag 不会立即触发许多过期。仅当下次访问该路径时才会发生失效。

    ¥unstable_expireTag only invalidates the cache when the path is next visited. This means calling unstable_expireTag with a dynamic route segment will not immediately trigger many expirations at once. The invalidation only happens when the path is next visited.

参考

¥Reference

参数

¥Parameters

unstable_expireTag(...tags: string[]): void;
  • tags:表示与要重新验证的数据关联的缓存标记的字符串参数。每个必须小于或等于 256 个字符。该值区分大小写。

    ¥tags: String arguments representing the cache tags associated with the data you want to revalidate. Must be less than or equal to 256 characters each. This value is case-sensitive.

你可以向 fetch 添加标签,如下所示:

¥You can add tags to fetch as follows:

fetch(url, { next: { tags: [...] } });

返回

¥Returns

unstable_expireTag 不返回值。

¥unstable_expireTag does not return a value.

示例

¥Examples

服务器动作

¥Server Action

你可以在服务器操作中调用 unstable_expireTag

¥You can invoke unstable_expireTag in a Server Action:

'use server'

import { unstable_expireTag } from 'next/cache'

export default async function submit() {
await addPost()
unstable_expireTag('posts', 'blog')
}
'use server'

import { unstable_expireTag } from 'next/cache'

export default async function submit() {
await addPost()
unstable_expireTag('posts', 'blog')
}

路由处理程序

¥Route Handler

你可以在路由处理程序中调用 unstable_expireTag

¥You can invoke unstable_expireTag in a Route Handler:

import type { NextRequest } from 'next/server'
import { unstable_expireTag } from 'next/cache'

export async function GET(request: NextRequest) {
const tag = request.nextUrl.searchParams.get('tag')
unstable_expireTag(tag)
return Response.json({ revalidated: true, now: Date.now() })
}
import { unstable_expireTag } from 'next/cache'

export async function GET(request) {
const tag = request.nextUrl.searchParams.get('tag')
unstable_expireTag(tag)
return Response.json({ revalidated: true, now: Date.now() })
}