Skip to main content

unstable_noStore

在版本 15 中,我们建议使用 connection 而不是 unstable_noStore

¥In version 15, we recommend using connection instead of unstable_noStore.

unstable_noStore 可用于以声明方式选择退出静态渲染并指示不应缓存特定组件。

¥unstable_noStore can be used to declaratively opt out of static rendering and indicate a particular component should not be cached.

import { unstable_noStore as noStore } from 'next/cache';

export default async function ServerComponent() {
noStore();
const result = await db.query(...);
...
}

很高兴知道:

¥Good to know:

  • unstable_noStore 相当于 fetch 上的 cache: 'no-store'

    ¥unstable_noStore is equivalent to cache: 'no-store' on a fetch

  • unstable_noStore 优于 export const dynamic = 'force-dynamic',因为它更细粒度并且可以在每个组件的基础上使用

    ¥unstable_noStore is preferred over export const dynamic = 'force-dynamic' as it is more granular and can be used on a per-component basis

  • unstable_cache 中使用 unstable_noStore 不会选择退出静态生成。相反,它将根据缓存配置来确定是否缓存结果。

    ¥Using unstable_noStore inside unstable_cache will not opt out of static generation. Instead, it will defer to the cache configuration to determine whether to cache the result or not.

用法

¥Usage

如果你不想将其他选项传递给 fetch,例如 cache: 'no-store'next: { revalidate: 0 },或者在 fetch 不可用的情况下,你可以使用 noStore() 替代所有这些用例。

¥If you prefer not to pass additional options to fetch, like cache: 'no-store', next: { revalidate: 0 } or in cases where fetch is not available, you can use noStore() as a replacement for all of these use cases.

import { unstable_noStore as noStore } from 'next/cache';

export default async function ServerComponent() {
noStore();
const result = await db.query(...);
...
}

版本历史

¥Version History

版本变化
v14.0.0unstable_noStore 推出。