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

04-第 3 层:全路由缓存 (Full Route Cache)

第 12 / 23 段:定时重新验证(Time-based Revalidation)

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

定时重新验证(Time-based Revalidation)

对于"内容偶尔更新"的页面,可以用 revalidate 实现定时更新:

// app/blog/page.tsx
export const revalidate = 3600; // 3600 秒 = 1 小时

export default async function BlogPage() {
  const res = await fetch("https://api.example.com/posts");
  const posts = await res.json();
  return <div>...</div>;
}

Stale-While-Revalidate 行为:

sequenceDiagram
    participant U1 as 用户1(0s)
    participant U2 as 用户2(30min)
    participant U3 as 用户3(61min)
    participant U4 as 用户4(62min)
    participant Cache as 全路由缓存

    U1->>Cache: 访问 /blog
    Cache-->>U1: 返回缓存 ✅

    U2->>Cache: 访问 /blog
    Cache-->>U2: 返回缓存 ✅(未过期)

    U3->>Cache: 访问 /blog
    Cache-->>U3: 返回旧缓存(先返回,不让用户等)
    Note over Cache: 后台触发重新渲染...

    U4->>Cache: 访问 /blog
    Cache-->>U4: 返回新缓存 ✅(已更新)

注意用户3拿到的仍然是旧数据——这就是 stale-while-revalidate 策略:先返回旧数据保证速度,后台异步更新,下一个用户才能拿到新数据。