【已解决】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;
  }
});
相关推荐
Bdygsl11 分钟前
前端开发:CSS(2)—— 选择器
前端·css
斯~内克18 分钟前
CSS包含块与百分比取值机制完全指南
前端·css·tensorflow
百万蹄蹄向前冲6 小时前
秋天的第一口代码,Trae SOLO开发体验
前端·程序员·trae
努力奋斗17 小时前
VUE-第二季-02
前端·javascript·vue.js
路由侠内网穿透7 小时前
本地部署 SQLite 数据库管理工具 SQLite Browser ( Web ) 并实现外部访问
运维·服务器·开发语言·前端·数据库·sqlite
一只韩非子7 小时前
程序员太难了!Claude 用不了?两招解决!
前端·claude·cursor
JefferyXZF7 小时前
Next.js项目结构解析:理解 App Router 架构(二)
前端·全栈·next.js
Sane7 小时前
react函数组件怎么模拟类组件生命周期?一个 useEffect 搞定
前端·javascript·react.js
gnip7 小时前
可重试接口请求
前端·javascript
若梦plus8 小时前
模块化与package.json
前端