import type { NextConfig } from "next"; import withPWAInit from "@ducanh2912/next-pwa"; /** * PWA 开关:仅在 production 启用。 * - dev 模式 SW 会干扰 HMR * - 测试/构建(vitest)也跳过 */ const isProd = process.env.NODE_ENV === "production"; const withPWA = withPWAInit({ // SW 输出目录(Next.js 静态资源根) dest: "public", // 仅 production 注入;dev 不启用避免 HMR 干扰 disable: !isProd, // 缓存策略:API 不缓存(业务 token 实时敏感),静态资源用默认 Workbox 策略 // 不显式声明 runtimeCaching,走 Workbox 默认(precache + NetworkFirst HTML + CacheFirst 静态) }); /** * Next.js 16 临时绕过 `/_global-error` prerender 的 workStore bug * 标记根 layout 不参与静态预渲染(global-error 会在请求时渲染) * https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config */ const nextConfig: NextConfig = { experimental: { // 关闭静态优化以规避 16.2.7 global-error prerender bug staticGenerationRetryCount: 0, }, }; export default withPWA(nextConfig);