Vite 部署后报错:Failed to fetch dynamically imported module XXXX

1,333 阅读1分钟

Vite 部署后报错:Failed to fetch dynamically imported module XXXX

问题描述:

版本情况

vuevite
3.3.44.4.9

使用Vite打包构建的项目,重新部署到生产。在部署期间用户一直停留在当前项目页面,部署完成后点击页面上的路由,报错! 刷新后恢复正常。

TypeError: Failed to fetch dynamically imported module: https://test.sndata-hz.com/aicloud/assets/appointment-5e30ed08.js

解决方式

import { createRouter, createWebHashHistory } from 'vue-router'
const router = createRouter({
  history: createWebHashHistory(),
  routes
})
router.beforeEach(async (to, from, next) => {
  // TODO
})
router.onError((error, to) => {
  const errors = ['Failed to fetch dynamically imported module'];
  if (errors.some((e) => error.message.includes(e))) {
    window.location = to.fullPath;
  }
});