fix(router): auto-reload on chunk load failure after rebuild

Add two layers of error handling for stale chunk imports:
- `vite:preloadError` listener in main.ts for Vite preload failures
- `router.onError` in router/index.ts for dynamic import failures during navigation

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
pull/878/head
DevDengChao 2026-03-05 15:31:02 +08:00
parent c4908548a1
commit 52e538aa43
2 changed files with 16 additions and 0 deletions

View File

@ -47,6 +47,12 @@ import { setupWangEditorPlugin } from '@/views/bpm/model/form/PrintTemplate'
import print from 'vue3-print-nb' // 打印插件
// 处理 Vite 预加载模块失败(如重新构建后 chunk 哈希变化),自动刷新页面
window.addEventListener('vite:preloadError', (event) => {
event.preventDefault()
window.location.reload()
})
// 创建实例
const setupAll = async () => {
const app = createApp(App)

View File

@ -19,6 +19,16 @@ const router = createRouter({
}
})
// 处理动态导入失败(如重新构建后 chunk 哈希变化),自动跳转到目标页面
router.onError((error, to) => {
if (
error.message.includes('Failed to fetch dynamically imported module') ||
error.message.includes('Importing a module script failed')
) {
window.location.assign(to.fullPath)
}
})
export const resetRouter = (): void => {
const resetWhiteNameList = ['Redirect', 'RedirectRoot', 'Login', 'NoFound', 'Home']
router.getRoutes().forEach((route) => {