serverComponentsHmrCache
实验性的 serverComponentsHmrCache
选项允许你在本地开发中的热模块替换 (HMR) 刷新之间缓存服务端组件中的 fetch
响应。这会导致更快的响应并减少计费 API 调用的成本。
默认情况下,HMR 缓存适用于所有 fetch
请求,包括那些带有 cache: 'no-store'
选项的请求。这意味着未缓存的请求在 HMR 刷新之间不会显示新数据。但是,缓存会在导航或完整页面重新加载时清除。
你可以通过在 next.config.js
文件中将 serverComponentsHmrCache
设置为 false
来禁用 HMR 缓存:
- TypeScript
- JavaScript
next.config.ts
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
experimental: {
serverComponentsHmrCache: false, // 默认为 true
},
}
export default nextConfig
next.config.js
/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
serverComponentsHmrCache: false, // 默认为 true
},
}
module.exports = nextConfig
提示:为了更好的可观察性,我们建议使用
logging.fetches
选项,它会在开发期间在控制台中记录 fetch 缓存命中和未命中。