【已解决】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;
  }
});
相关推荐
Cache技术分享4 分钟前
99. Java 继承(Inheritance)
前端·后端
SleepyZone13 分钟前
Cline 源码浅析 - 从输入到输出
前端·ai编程·cline
Struggler28116 分钟前
pinia-基于monorepo的项目结构管理
前端
Struggler28120 分钟前
SSE的使用
前端
用户58061393930027 分钟前
前端文件下载实现深度解析:Blob与ObjectURL的完美协作
前端
Lin866630 分钟前
Vue 3 + TypeScript 组件类型推断失败问题完整解决方案
前端
coding随想30 分钟前
从零开始:前端开发者的SEO优化入门与实战
前端
前端工作日常33 分钟前
我理解的JSBridge
前端
Au_ust33 分钟前
前端模块化
前端
顺丰同城前端技术团队33 分钟前
还不会用 Charles?最后一遍了啊!
前端