【已解决】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;
  }
});
相关推荐
小满zs2 小时前
React-router v7 第五章(路由懒加载)
前端·react.js
Aotman_2 小时前
Vue el-from的el-form-item v-for循环表单如何校验rules(二)
前端·javascript·vue.js
BillKu3 小时前
Vue3父子组件数据双向绑定示例
javascript·vue.js·elementui
在无清风4 小时前
Java实现Redis
前端·windows·bootstrap
_一条咸鱼_5 小时前
Vue 配置模块深度剖析(十一)
前端·javascript·面试
yechaoa6 小时前
Widget开发实践指南
android·前端
前端切图仔0017 小时前
WebSocket 技术详解
前端·网络·websocket·网络协议
JarvanMo7 小时前
关于Flutter架构的小小探讨
前端·flutter
前端开发张小七7 小时前
每日一练:4.有效的括号
前端·python
顾林海8 小时前
Flutter 图标和按钮组件
android·开发语言·前端·flutter·面试