【已解决】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;
  }
});
相关推荐
小土豆_77710 小时前
Owl 2.8.1 核心语法速查表(新手专用)
前端·odoo/owl
firstacui10 小时前
LVS三种模式搭建
前端·chrome
wanzhong233310 小时前
开发日记13-响应式变量
开发语言·前端·javascript·vue
代码游侠10 小时前
学习笔记——文件传输工具配置与Makefile详解
运维·前端·arm开发·笔记·学习
踢球的打工仔10 小时前
typescript-类的静态属性和静态方法
前端·javascript·typescript
匠心网络科技10 小时前
前端框架-Vue双向绑定核心机制全解析
前端·javascript·vue.js·前端框架
Jinuss10 小时前
源码分析之React中的FiberRoot节点属性介绍
前端·javascript·react.js
自回归向前看11 小时前
2020-25 Js ES新增加特性
前端·javascript
wanzhong233311 小时前
开发日记14-vite配置多环境
服务器·前端·vue
Jinuss11 小时前
源码分析之React中的Fiber节点介绍
前端·javascript·react.js