unstable_noStore
unstable_noStore 可用於宣告式地選擇退出靜態渲染,並表示特定元件不應被快取。
import { unstable_noStore as noStore } from 'next/cache';
export default async function Component() {
noStore();
const result = await db.query(...);
...
}小知識:
unstable_noStore等同於在fetch中使用cache: 'no-store'- 相較於
export const dynamic = 'force-dynamic',建議優先使用unstable_noStore,因為它更細緻且可以基於每個元件單獨使用
- 在
unstable_cache中使用unstable_noStore並不會退出靜態生成。相反地,它會根據快取配置來決定是否快取結果。
使用方式
如果您不想在 fetch 中傳遞額外選項,例如 cache: 'no-store' 或 next: { revalidate: 0 },可以使用 noStore() 來替代這些使用情境。
import { unstable_noStore as noStore } from 'next/cache';
export default async function Component() {
noStore();
const result = await db.query(...);
...
}版本歷史
| 版本 | 變更內容 |
|---|---|
v14.0.0 | 引入 unstable_noStore 功能。 |