Skip to main content

staleTimes(实验)

警告:staleTimes 配置是一项实验性功能。这种配置策略将来可能会发生变化。

¥Warning: The staleTimes configuration is an experimental feature. This configuration strategy will likely change in the future.

staleTimes 是一项实验性功能,可在 客户端路由缓存 中启用页面段的缓存。

¥staleTimes is an experimental feature that enables caching of page segments in the client-side router cache.

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

¥You can enable this experimental feature and provide custom revalidation times by setting the experimental staleTimes flag:

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

module.exports = nextConfig

staticdynamic 属性对应于基于不同类型的 链接预取 的时间段(以秒为单位)。

¥The static and dynamic properties correspond with the time period (in seconds) based on different types of link prefetching.

  • Link 上的 prefetch 属性未指定或设置为 false 时,将使用 dynamic 属性。

    ¥The dynamic property is used when the prefetch prop on Link is left unspecified or is set to false.

    • 默认:0 秒(未缓存)

      ¥Default: 0 seconds (not cached)

  • Link 上的 prefetch 属性设置为 true 或调用 router.prefetch 时,使用 static 属性。

    ¥The static property is used when the prefetch prop on Link is set to true, or when calling router.prefetch.

    • 默认:5 分钟

      ¥Default: 5 minutes

很高兴知道:

¥Good to know:

  • 加载边界 被认为在此配置中定义的 static 周期内可重复使用。

    ¥Loading boundaries are considered reusable for the static period defined in this configuration.

  • 这不会影响 部分渲染,这意味着共享布局不会在每次导航时自动重新获取,只会重新获取发生变化的页面段。

    ¥This doesn't affect partial rendering, meaning shared layouts won't automatically be refetched on every navigation, only the page segment that changes.

  • 这不会改变 后退/前进缓存 行为以防止布局偏移并防止丢失浏览器滚动位置。

    ¥This doesn't change back/forward caching behavior to prevent layout shift and to prevent losing the browser scroll position.

  • 此配置的不同属性涉及 "liveness" 的变量级别,并且与段本身选择静态还是动态渲染无关。换句话说,当前 static 默认值 5 分钟表明数据由于很少重新验证而感觉静态。

    ¥The different properties of this config refer to variable levels of "liveness" and are unrelated to whether the segment itself is opting into static or dynamic rendering. In other words, the current static default of 5 minutes suggests that data feels static by virtue of it being revalidated infrequently.

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

¥You can learn more about the Client Router Cache here.

版本历史

¥Version History

版本变化
v15.0.0staleTimes 启用并配置页面段的持续时间
v14.2.0引入实验性 staleTimes