【已解决】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;
  }
});
相关推荐
阿里巴啦11 小时前
照片隐私清理工具:基于Taro 4 + Vue 3 + piexifjs开发实践项目
vue.js·照片隐私清除·piexifjs·exif 解析
贾修行11 小时前
Kestrel:.NET 的高性能 Web 服务器探秘
服务器·前端·kestrel·.net·net core·web-server·asp.net-core
吃吃喝喝小朋友11 小时前
HTML DOM
前端·javascript·html
HWL567911 小时前
HTML中,<video> 和 <source> 标签
前端·javascript·html
球球不吃虾11 小时前
分享一个简单的交互式塔罗牌抽牌应用
前端·vue
2501_9481201511 小时前
中职动漫设计与制作专业实训方案研究
前端·人工智能·语言模型·自然语言处理·架构
小小鸟00811 小时前
前端 RBAC基于角色的权限控制(按钮级别)
前端
学习java的小库里11 小时前
EasyExcel复杂导出
java·前端
muddjsv11 小时前
前端开发通用全流程:从需求到上线,步步拆解
前端
Mr Xu_11 小时前
从零实战!使用 Mars3D 快速构建水利监测 WebGIS 系统
前端·3d·webgis