From c6597880227cd9f44632cb593a4c9c36ce07ee0a Mon Sep 17 00:00:00 2001 From: yangjianbo Date: Fri, 16 Jan 2026 16:20:10 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E5=89=8D=E7=AB=AF=E8=B7=AF=E7=94=B1):=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20chunk=20=E5=8A=A0=E8=BD=BD=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E8=87=AA=E5=8A=A8=E6=81=A2=E5=A4=8D=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 检测动态导入模块加载失败错误 - 自动刷新页面获取最新资源 - 使用 sessionStorage 防止无限刷新循环(10秒冷却) - 解决前端重新部署后用户缓存导致的加载失败问题 Co-Authored-By: Claude Opus 4.5 --- frontend/src/router/index.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/frontend/src/router/index.ts b/frontend/src/router/index.ts index 7e929400..9c507797 100644 --- a/frontend/src/router/index.ts +++ b/frontend/src/router/index.ts @@ -400,9 +400,33 @@ router.beforeEach((to, _from, next) => { /** * Navigation guard: Error handling + * Handles dynamic import failures caused by deployment updates */ router.onError((error) => { console.error('Router error:', error) + + // Check if this is a dynamic import failure (chunk loading error) + const isChunkLoadError = + error.message?.includes('Failed to fetch dynamically imported module') || + error.message?.includes('Loading chunk') || + error.message?.includes('Loading CSS chunk') || + error.name === 'ChunkLoadError' + + if (isChunkLoadError) { + // Avoid infinite reload loop by checking sessionStorage + const reloadKey = 'chunk_reload_attempted' + const lastReload = sessionStorage.getItem(reloadKey) + const now = Date.now() + + // Allow reload if never attempted or more than 10 seconds ago + if (!lastReload || now - parseInt(lastReload) > 10000) { + sessionStorage.setItem(reloadKey, now.toString()) + console.warn('Chunk load error detected, reloading page to fetch latest version...') + window.location.reload() + } else { + console.error('Chunk load error persists after reload. Please clear browser cache.') + } + } }) export default router