【已解决】Vite 部署后报错:Failed to fetch dynamically imported module

问题描述:

使用Vite打包构建的项目,重新部署到生产。在部署期间用户一直停留在当前项目页面,部署完成后点击页面上的路由,报错! 刷新后恢复正常。

Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.

TypeError: Failed to fetch dynamically imported module: xxx/assets/index-4f5b61b3.js

在Vite的github找到下面这个issue,一模一样的问题

github.com/vitejs/vite...

Since switching to Vite we noticed a new production issue, where sometimes users are encountering an error if we deploy while they have an active session:

这个issue留言还比较多,同时也找到了解决方法:

js 复制代码
// Hard-reload the page when chunk load errors match the navigation error
const routerInstance = router();
routerInstance.onError((error, to) => {
  const errors = ['Failed to fetch dynamically imported module', 'Unable to preload CSS'];

  if (errors.some((e) => error.message.includes(e))) {
    window.location = to.fullPath;
  }
});
相关推荐
1-1=01 分钟前
Vue 全局事件总线
前端·javascript·vue.js
唐老板33 分钟前
前端和后端,转 AI 全栈怎么转
前端·后端
daguanren1 小时前
同一份小程序源码,怎么切到 Vite+?weapp-vite 的工具链选择设计
微信小程序·vite
程序员鱼皮1 小时前
Antigravity 保姆级实战教程,从 0 到 1 万字干货!
前端·后端·ai编程
徐小夕1 小时前
JitKnow AI知识库:把任何文档,一键变成可访问和可配置的Wiki站点
前端·vue.js·github
狮子座的男孩1 小时前
javascript 基础 es6:2、模板字符串,简化对象写法,箭头函数,箭头函数实践案例
前端·javascript·经验分享·es6·模板字符串·箭头函数·简化对象
hackhase1 小时前
Vue3 + Vite 纯 SPA 的 SEO 工程化:构建期预渲染 21 个页面
javascript·vite
夏与冰2 小时前
Vue2项目动态路由的实现思路
vue.js
陈如水2 小时前
AI Agent 主流架构
前端·javascript·react.js
Amos_Web2 小时前
通过 Rspack 源码学 Rust(七):Copy 与 Clone,什么时候复制,什么时候移动
前端