staleTimes

staleTimes 是一项实验性功能,用于在 客户端路由缓存 (Client Router Cache) 中缓存页面片段。

您可以通过设置实验性的 staleTimes 标志来启用此功能并提供自定义的重新验证时间:

next.config.js
/** @type {import('next').NextConfig} */
const nextConfig = {
  experimental: {
    staleTimes: {
      dynamic: 30,
      static: 180,
    },
  },
}

module.exports = nextConfig

staticdynamic 属性分别对应基于不同类型 链接预取 (link prefetching) 的时间周期(以秒为单位)。

  • dynamic 属性用于页面既非静态生成也未完全预取的情况(例如 prefetch={true} 未设置时)
    • 默认值:0 秒(不缓存)
  • static 属性用于静态生成的页面,或当 Link 组件的 prefetch 属性设为 true 时,亦或调用 router.prefetch
    • 默认值:5 分钟

须知:

您可以在此处了解更多关于客户端路由缓存的信息。

版本历史

版本变更内容
v15.0.0dynamicstaleTimes 默认值从 30 秒改为 0 秒
v14.2.0实验性 staleTimes 功能引入

On this page