【已解决】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;
  }
});
相关推荐
喝拿铁写前端30 分钟前
AI 时代,ESLint 应该怎么做得更好
前端·人工智能·代码规范
妙码生花1 小时前
从 PHP 到 AI + Golang,程序员自救转型手记(二十七):管理员登录验证码开关
前端·后端·ai编程
尼斯湖皮皮怪1 小时前
iceCoder 记忆系统深度分析
前端·javascript
用户25577655743001 小时前
Cloudflare Pages 部署 Vite 项目踩坑记
前端
谢尔登1 小时前
通过Semi Design思考前端组件AI化
前端·人工智能
小蚂蚁i1 小时前
把 TypeScript 内置工具类型彻底搞懂,顺便自己封几个好用的
前端·typescript
维基框架1 小时前
当Prompt编辑变成训练过程:SkillOpt的逆直觉方案
前端
KaMeidebaby1 小时前
卡梅德生物技术快报|小 RNA 适配体合成 + 多方法亲和力表征全流程标准化操作手册
前端·网络·数据库·人工智能·算法
OpenTiny社区2 小时前
Coding Agent 到底是怎么工作的,它又为什么会在复杂代码库里失真?
前端·github·agent
奇奇怪怪的2 小时前
Graph RAG:知识图谱增强检索
前端