【已解决】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;
  }
});
相关推荐
IT_陈寒6 分钟前
JavaScript性能优化:7个被低估的V8引擎技巧让你的代码提速50%
前端·人工智能·后端
bigHead-6 分钟前
前端双屏显示与通信
开发语言·前端·javascript
顾安r12 分钟前
1.1 脚本网页 战推棋
java·前端·游戏·html·virtualenv
一颗小青松13 分钟前
vue 腾讯地图经纬度转高德地图经纬度
前端·javascript·vue.js
Justin3go8 小时前
HUNT0 上线了——尽早发布,尽早发现
前端·后端·程序员
怕浪猫9 小时前
第一章 JSX 增强特性与函数组件入门
前端·javascript·react.js
铅笔侠_小龙虾9 小时前
Emmet 常用用法指南
前端·vue
钦拆大仁9 小时前
跨站脚本攻击XSS
前端·xss
VX:Fegn089510 小时前
计算机毕业设计|基于springboot + vue校园社团管理系统(源码+数据库+文档)
前端·数据库·vue.js·spring boot·后端·课程设计
ChangYan.11 小时前
直接下载源码但是执行npm run compile后报错
前端·npm·node.js