您的位置:首页 > 财经 > 金融 > react lazy加载资源找不到的问题

react lazy加载资源找不到的问题

2024/10/5 13:49:27 来源:https://blog.csdn.net/problc/article/details/141950637  浏览:    关键词:react lazy加载资源找不到的问题

在 Umi 4 中,默认按页拆包进行优化,实现每个页面只需加载最少的 js 资源,这会产生很多异步 js 分包。通常我们会开启 hash: true 构建,将 js / css 等资源做长期缓存,而 html 不缓存。

然而,在版本发布时,如果有用户在旧的应用 html 上加载新的页面,会导致旧资源 xxx.[hash].js 加载不到。比如在整个替换 oss 存储内容的情况下,因为新版本已经发布,旧的 hash 文件不存在了。
在这里插入图片描述

下面为大家介绍两种解决方案:

一、加载失败自动重试方案

可以考虑 patch React.lazy 加载方法,遇到加载失败后自动 reload 页面重试。以下是自动重试的示例代码,若阻塞超过 10s,则弹出报错弹窗需用户手动刷新加载:

// src/global.tsximport { Modal, Result } from 'antd';
import React from 'react';const originLazy = React.lazy;React.lazy = (factory) => {return originLazy(() => factory().catch(loadError));
};
let hasError = false;
function loadError(): { default: React.ComponentType<unknown> } {const time = Number(window.location.hash.match(/#s(\d+)/)?.[1]);const now = Date.now();const isReloading =!time || time + 10000 < now;if (isReloading) {window.location.hash = `#s${now}`;window.location.reload();}const module = {default: () => {if (hasError) {return null;}hasError = true;return (<ModalopencancelButtonProps={{style: {display: 'none',},}}onOk={() => {window.location.reload();}}okText="Reload"><Result title="Generic error message" status="error">{`Oops, something went wrong.`}</Result></Modal>);},};return module;
}

参考来源:https://twitter.com/cpojer/status/1730141247900459166。

另外,也可参考 Vite 的 preload error 实现,自行在 loadError 内 dispatch 事件,之后在需要监听的地方处理即可。

二、自动检测新版本是否发布方案

还可参考https://github.com/umijs/umi/issues/10171 中的方案,在后台轮询 html 内容,在 html 中或其他位置维护新的版本标识,发版后将自动提示用户刷新页面。

注:添加版本标识的方案多样,如在 api.onBuildHtmlComplete 构建完成后手动修改 html,或 headScripts 手动添加等:

//.umirc.tsheadScripts: [{content: `// version: 1.2.3`}],

此外,“version-rocket”这个库也可以参考。单独开了一个工作线程,后台轮询比对版本标识,逻辑相同。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com