【已解决】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;
  }
});
相关推荐
全栈前端老曹几秒前
【ReactNative】页面跳转与参数传递 - navigate、push 方法详解
前端·javascript·react native·react.js·页面跳转·移动端开发·页面导航
用泥种荷花1 小时前
【前端学习AI】Python环境搭建
前端
老华带你飞1 小时前
考试管理系统|基于java+ vue考试管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
_Kayo_1 小时前
React上绑定全局方法
前端·javascript·react.js
愈努力俞幸运1 小时前
chrome 扩展(插件)开发入门教程
前端·chrome
练习前端两年半1 小时前
【Vue3 高级技巧】函数重载+Watch:打造类型安全的通用事件监听 Hook
前端·javascript·vue.js
一只小鸟儿2 小时前
门户短信发送验证码及验证功能
前端·javascript·jquery
elangyipi1232 小时前
pnpm :下一代包管理工具的原理与实践
前端·npm
代码的奴隶(艾伦·耶格尔)2 小时前
Sentinel限流熔断
java·前端·sentinel
talenteddriver2 小时前
mysql: MySQL中between子句和limit子句的区别
前端·javascript·数据库