diff --git a/service/core/router/index.cjs b/service/core/router/index.cjs index ecb84d6..073aaee 100644 --- a/service/core/router/index.cjs +++ b/service/core/router/index.cjs @@ -94,10 +94,29 @@ const matchFile = async (request, response) => { if (fs.existsSync(mappedPath)) { targetPath = mappedPath } else if (cleanPath.startsWith('/assets/')) { - response.writeHead(404, { 'Content-Type': 'text/plain' }) - response.end('Asset Not Found') - resolve(true) - return + // 如果请求的是带 hash 的旧文件,尝试摘除 -[hash] 请求固定文件 + const filename = cleanPath.split('/').pop() + const parts = filename.split('.') + const ext = parts.pop() + const nameParts = parts.join('.').split('-') + if (nameParts.length > 1) { + nameParts.pop() // 移除最后的 hash + const baseName = nameParts.join('-') + const fallbackPath = dist + '/assets/' + baseName + '.' + ext + if (fs.existsSync(fallbackPath)) { + targetPath = fallbackPath + } else { + response.writeHead(404, { 'Content-Type': 'text/plain' }) + response.end('Asset Not Found') + resolve(true) + return + } + } else { + response.writeHead(404, { 'Content-Type': 'text/plain' }) + response.end('Asset Not Found') + resolve(true) + return + } } } fs.readFile(targetPath, (err, file) => { diff --git a/src/router/index.js b/src/router/index.js index 8f208c3..fb51863 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -48,25 +48,14 @@ router.beforeEach(async (to, from, next) => { router.onError((error) => { const pattern = /Failed to fetch dynamically imported module|Importing a module script failed/i if (pattern.test(error?.message || '')) { - if (window._chunkErrorHandled) return - window._chunkErrorHandled = true - - const forceReload = () => { - // 不再重载当前错误路径,直接跳回首页,并带上时间戳跳出 CDN 缓存 - window.location.href = window.location.origin + '/?t=' + Date.now() - } - - const cleanAndReload = async () => { - // 暴力清空所有的 Cache API,防止 SW 缓存 index.html 等 - if (window.caches) { - try { - const keys = await window.caches.keys() - for (const key of keys) { - await window.caches.delete(key) - } - } catch (e) {} + const isReloaded = sessionStorage.getItem('chunk_reload_flag') + if (!isReloaded) { + sessionStorage.setItem('chunk_reload_flag', 'true') + const forceReload = () => { + const url = new URL(window.location.href) + url.searchParams.set('t', Date.now()) + window.location.href = url.href } - if ('serviceWorker' in navigator) { navigator.serviceWorker.getRegistrations().then(registrations => { for (const registration of registrations) { @@ -78,12 +67,6 @@ router.onError((error) => { forceReload() } } - - if (window.confirm('系统检测到版本更新,当前页面数据已过期。点击【确定】立即刷新体验最新版本!')) { - cleanAndReload() - } else { - window._chunkErrorHandled = false - } } }) diff --git a/vite.config.js b/vite.config.js index 369958c..e2cfe3b 100644 --- a/vite.config.js +++ b/vite.config.js @@ -41,6 +41,11 @@ export default defineConfig({ build: { chunkSizeWarningLimit: 2000, rollupOptions: { + output: { + entryFileNames: 'assets/[name].js', + chunkFileNames: 'assets/[name].js', + assetFileNames: 'assets/[name].[ext]' + }, onwarn(warning, warn) { if (warning.code === 'UNRESOLVED_IMPORT' || warning.code === 'UNUSED_EXTERNAL_IMPORT') return; warn(warning);