缓存的标记方式:标签 vs 路径(续)
标签不仅限于数据缓存。 在 Next.js 16 中,
cacheTag()可以用在任何use cache标记的缓存单元上——不只是fetch请求,还包括整个页面、单个组件、甚至一个普通的 async 函数:// 给 fetch 请求打标签(数据缓存) const res = await fetch("/api/posts", { next: { tags: ["posts"] }, }); // 给 use cache 组件/函数打标签(Next.js 16+) import { cacheTag } from "next/cache"; export async function PostList() { "use cache"; cacheTag("posts"); // 标签可以用于组件级缓存 const posts = await db.post.findMany(); return posts.map((p) => <article>{p.title}</article>); }这意味着
updateTag('posts')或revalidateTag('posts', 'max')会同时失效所有打了'posts'标签的缓存——无论它是 fetch 请求、数据库查询还是整个组件的渲染结果。标签是贯穿整个缓存体系的统一失效机制。