实战案例:博客编辑的完整缓存策略
// 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>
);
}