HTML 原文 · 完整 Markdown · 纯文本 · 分段目录 · ← 上一段 · 下一段 →

05-第 4 层:路由器缓存 (Router Cache)

第 14 / 17 段:实战案例:博客编辑的完整缓存策略

Next.js 缓存机制全解析 · 发布 2026-02-20 · 更新 2026-11-11 · 标签:Next.js、缓存、前端架构、React

实战案例:博客编辑的完整缓存策略

// app/posts/page.tsx — 列表页
export default async function PostsPage() {
  const res = await fetch("https://api.example.com/posts", {
    next: { tags: ["posts"] },
    cache: "force-cache",
  });
  const posts = await res.json();

  return (
    <div>
      {posts.map((post) => (
        <Link key={post.id} href={`/posts/${post.id}`}>
          <h2>{post.title}</h2>
        </Link>
      ))}
    </div>
  );
}
// app/posts/[id]/page.tsx — 详情页
export default async function PostPage({
  params,
}: {
  params: Promise<{ id: string }>;
}) {
  const { id } = await params;
  const res = await fetch(`https://api.example.com/posts/${id}`, {
    next: { tags: ["posts", `post-${id}`] },
    cache: "force-cache",
  });
  const post = await res.json();

  return (
    <div>
      <h1>{post.title}</h1>
      <p>{post.content}</p>
      <Link href={`/posts/${id}/edit`}>编辑</Link>
    </div>
  );
}