代码示例
示例 1:博客文章列表(定时缓存)
// app/blog/page.tsx
export default async function BlogPage() {
// 每小时重新验证一次
const res = await fetch("https://api.example.com/posts", {
next: { revalidate: 3600 },
});
const posts = await res.json();
return (
<div>
{posts.map((post) => (
<article key={post.id}>
<h2>{post.title}</h2>
<p>{post.excerpt}</p>
</article>
))}
</div>
);
}
示例 2:用户个人信息(从不缓存)
// app/profile/page.tsx
export default async function ProfilePage() {
// 每次都重新获取
const res = await fetch("https://api.example.com/user/me", {
cache: "no-store",
headers: {
Authorization: `Bearer ${token}`,
},
});
const user = await res.json();
return (
<div>
<h1>{user.name}</h1>
<p>{user.email}</p>
</div>
);
}
示例 3:产品列表(永久缓存)