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 allows configuring the invalidation period of the client router cache.

此配置选项自 v14.2.0-canary.53 起可用。

¥This configuration option is available as of v14.2.0-canary.53.

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

¥You can enable this experimental feature & 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 属性未指定时,使用 dynamic 属性。

    ¥The dynamic property is used when the prefetch prop on Link is left unspecified.

    • 默认:30 秒

      ¥Default: 30 seconds

  • 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 disable partial rendering support, meaning shared layouts won't automatically be refetched every navigation, only the new segment data.

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

    ¥This doesn't change back/forward caching behavior to prevent layout shift & 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.