unstable_noStore
这是一个遗留的 API,不再推荐使用。它仍然受支持以实现向后兼容。
在版本 15 中,我们建议使用 connection
而不是 unstable_noStore
。
unstable_noStore
可以用于声明式地选择退出静态渲染,并指示特定组件不应被缓存。
import { unstable_noStore as noStore } from 'next/cache';
export default async function ServerComponent() {
noStore();
const result = await db.query(...);
...
}
须知:
unstable_noStore
等同于fetch
上的cache: 'no-store'
unstable_noStore
优于export const dynamic = 'force-dynamic'
,因为它更精细,并且可以按组件使用
- 在
unstable_cache
内部使用unstable_noStore
不会选择退出静态生成。相反,它将遵从缓存配置来确定是否缓存结果。
用法
如果您不想向 fetch
传递额外的选项,例如 cache: 'no-store'
,next: { revalidate: 0 }
,或者在 fetch
不可用的情况下,您可以使用 noStore()
来替代所有这些用例。
import { unstable_noStore as noStore } from 'next/cache';
export default async function ServerComponent() {
noStore();
const result = await db.query(...);
...
}
版本历史
版本 | 变更 |
---|---|
v15.0.0 | unstable_noStore 已弃用,建议使用 connection 。 |
v14.0.0 | 引入了 unstable_noStore 。 |
这是否有帮助?