解决vite项目发版后浏览器缓存问题

问题:更新环境的版本后,再次点开时是旧的前端

下图中,当时最新的发布版本还是 26 号,却显示 22号的

image.png

打开某页面时,页面渲染失败,如下所示

image.png

原因:

vite 默认配置后,打包后 css 和 js 的名字后面都加了哈希值,不会有缓存问题。
但是 index.html 是有缓存的,其中加载的 css\js 文件已经不对了。 于是,出现报错 TypeError: Failed to fetch dynamically imported module。

image.png

解决方案:

① 处理 TypeError: Failed to fetch dynamically imported module

相关 issue:

在 ErrorBoundary 中,捕获到错误这种错误后,自动刷新页面 (不过会看见闪屏,体验感不太好)

 const fetchResourcesErrors = ['Failed to fetch dynamically imported module', 'Importing a module script failed']
   if (fetchResourcesErrors.some((item) => isString(error?.message) && error.message?.includes(item))) {
     window.location.reload()
   }

② 不缓存 index.html

a.前端在 .html 页面加 meta 标签

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache, no-store, must-revalidate">
    <meta http-equiv="expires" content="0">

b.后端 nginx 配置,让 index.html 不缓存

add_header Cache-Control "no-cache, no-store";

相关文章:

  • 12
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值