实战建议
博客 / 文档站(内容不常更新)
// 使用 tags + revalidate 双保险
export const revalidate = 3600; // 兜底:最多 1 小时更新
export default async function BlogPage() {
const res = await fetch("https://api.example.com/posts", {
next: { tags: ["posts"] },
cache: "force-cache",
});
const posts = await res.json();
return <div>...</div>;
}
// 发布新文章时主动失效
// app/actions.ts
"use server";
import { updateTag } from "next/cache";
export async function publishPost() {
await db.post.create({ ... });
updateTag("posts"); // 立即失效,用户马上看到新文章
}
管理后台(数据频繁变化)
// 直接放弃全路由缓存
export const dynamic = "force-dynamic";
export default async function AdminPage() {
const res = await fetch("https://api.example.com/stats");
const stats = await res.json();
return <div>...</div>;
}
电商网站(混合策略)