diff --git a/src/service-worker.ts b/src/service-worker.ts index 0ad855f1..5d4fef5f 100644 --- a/src/service-worker.ts +++ b/src/service-worker.ts @@ -12,7 +12,18 @@ declare let self: ServiceWorkerGlobalScope & { // 缓存版本控制 const RESOURCE_VERSION = 'V2' -const CACHE_VERSION = `${__APP_VERSION__}-${__BUILD_TIME__}` // 开发环境下无法使用此环境变量,生产环境正常 +// 开发态 dev-sw 可能拿不到 Vite define 注入;仅在开发环境做 dev 兜底 +const hasAppVersion = typeof __APP_VERSION__ !== 'undefined' +const hasBuildTime = typeof __BUILD_TIME__ !== 'undefined' +const isDev = import.meta.env.DEV + +if (!isDev && (!hasAppVersion || !hasBuildTime)) { + throw new Error('[SW] Missing __APP_VERSION__ or __BUILD_TIME__ in production build') +} + +const appVersion = hasAppVersion ? __APP_VERSION__ : 'dev' +const buildTime = hasBuildTime ? __BUILD_TIME__ : 'dev' +const CACHE_VERSION = `${appVersion}-${buildTime}` // 启用导航预载 navigationPreload.enable() diff --git a/src/stores/global.ts b/src/stores/global.ts index 1ab0b0d4..cc434d93 100644 --- a/src/stores/global.ts +++ b/src/stores/global.ts @@ -23,6 +23,14 @@ export const useGlobalSettingsStore = defineStore('globalSettings', { // 检查版本更新 if (result.FRONTEND_VERSION) { + const isBackendDev = Boolean(result.BACKEND_DEV) + const skipVersionCheck = import.meta.env.DEV || isBackendDev + + if (skipVersionCheck) { + console.log('[VersionChecker] 开发环境下跳过版本一致性检查') + return + } + const { checkVersion } = useVersionChecker() await checkVersion(result.FRONTEND_VERSION) }