【已解决】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;
  }
});
相关推荐
0思必得012 小时前
[Web自动化] 爬虫实例(获取时光网某个年度电影数据)
前端·爬虫·python·selenium·自动化
Devlive 开源社区12 小时前
技术日报|React视频工具Remotion三连冠日增1374星破3万,Apple Silicon语音库mlx-audio爆火登榜第二
前端·react.js·前端框架
m0_7482299912 小时前
Vue3高效学习路线全攻略
前端·javascript·vue.js
谢尔登12 小时前
React架构演变
前端·react.js·架构
木辰風13 小时前
vue在IE浏览器下父页面向子页面传输对象时数据丢失
前端·javascript·html
小雨青年13 小时前
Cursor 项目实战:AI播客策划助手(四)—— 产品发布与交付收尾
前端·人工智能
麻辣翅尖13 小时前
【vue】基于 pdf.js 实现 pdf 文件预览
vue.js·pdf.js
@呵呵13 小时前
上传图片裁剪
开发语言·javascript·vue.js
晚霞的不甘13 小时前
Flutter for OpenHarmony《淘淘购物》主页点击跳转功能详解:从 UI 响应到页面导航的完整实现
前端·flutter·ui·搜索引擎·前端框架·交互
cooldream200913 小时前
前端技术架构详解:Vue 3 + TypeScript + Vite 在具身 AI 系统中的实践
前端·架构·typescript