92 lines
4.1 KiB
TypeScript
92 lines
4.1 KiB
TypeScript
/**
|
||
* 渲染期兜底 + 路由分包加载失败自愈。
|
||
*
|
||
* 为什么必须有它:页面全部改成路由级懒加载之后,「点一个页面」多了一次网络请求。
|
||
* 重新部署过一次,浏览器里那份 index.html 还在引用被替换掉的旧哈希 chunk,
|
||
* 点进去就是 `Failed to fetch dynamically imported module`;React.lazy 在渲染期
|
||
* 抛出的这个 rejection 要是没人接,会一路冒到根、整棵树被卸载 —— 用户看到的
|
||
* 是一片白屏,而且他手上的壳还是缓存的旧版,刷新也不一定自己好。
|
||
* 所以这里做两件事:① chunk 加载失败先自动重刷一次(只一次,避免刷新循环);
|
||
* ② 真兜住了就给出带说明的兜底页,而不是 console 里一行红字。
|
||
*
|
||
* React 19 至今只有 class 组件能当错误边界,所以这是全仓唯一的 class。
|
||
* 用法:包在 <Suspense> 外面,并用 pathname 当 key —— 换页时边界自动复位,
|
||
* 不需要额外的「清除错误」状态。
|
||
*/
|
||
import { Component, type ErrorInfo, type ReactNode } from 'react'
|
||
import { Result, Typography } from 'antd'
|
||
import { Button, Notice } from '../components/ui'
|
||
import { IconProjects, IconReload } from '../components/icons'
|
||
|
||
/** 自动重刷只允许发生一次:记时间戳而不是布尔值,隔一阵子的问题还能再自愈一回 */
|
||
const RELOAD_KEY = 'nex.chunk.reload.at'
|
||
const RELOAD_WINDOW_MS = 30_000
|
||
|
||
/** 各家浏览器对「动态 import 失败」的措辞不一样,统一按语义匹配 */
|
||
const CHUNK_FAILURE = /dynamically imported module|importing a module script failed|error loading dynamically|failed to fetch/i
|
||
|
||
export function isChunkLoadFailure(error: Error | null): boolean {
|
||
return !!error && CHUNK_FAILURE.test(`${error.name} ${error.message}`)
|
||
}
|
||
|
||
/** 抢这一次重刷机会;隐私模式下 sessionStorage 会抛,那种时候宁可放它过去。 */
|
||
function claimReload(): boolean {
|
||
const now = Date.now()
|
||
try {
|
||
const last = Number(sessionStorage.getItem(RELOAD_KEY) || 0)
|
||
if (last && now - last < RELOAD_WINDOW_MS) return false
|
||
sessionStorage.setItem(RELOAD_KEY, String(now))
|
||
return true
|
||
} catch {
|
||
return true
|
||
}
|
||
}
|
||
|
||
type Props = { children: ReactNode }
|
||
type State = { error: Error | null }
|
||
|
||
export default class ErrorBoundary extends Component<Props, State> {
|
||
state: State = { error: null }
|
||
|
||
static getDerivedStateFromError(error: Error): State {
|
||
return { error }
|
||
}
|
||
|
||
componentDidCatch(error: Error, info: ErrorInfo) {
|
||
// 堆栈留在 console:兜底页只给人话,把 componentStack 摊给用户看没有意义
|
||
console.error('[nex] 页面渲染失败', error, info.componentStack)
|
||
if (isChunkLoadFailure(error) && claimReload()) window.location.reload()
|
||
}
|
||
|
||
render() {
|
||
const { error } = this.state
|
||
if (!error) return this.props.children
|
||
const chunk = isChunkLoadFailure(error)
|
||
return (
|
||
<Result
|
||
status="error"
|
||
title={chunk ? '这一页的代码没拿到' : '这一页没能画出来'}
|
||
subTitle={chunk
|
||
? '多半是服务端刚重新部署过,浏览器还在用旧版页面清单。重新加载通常就好了;一直不好就把本页地址发我。'
|
||
: '页面在渲染时抛了异常。先重新加载一次;如果每次进来都这样,请附上下面这行提示。'}
|
||
extra={[
|
||
<Button type="primary" key="reload" icon={<IconReload />} onClick={() => window.location.reload()}>
|
||
重新加载本页
|
||
</Button>,
|
||
<Button key="home" href="/" icon={<IconProjects />}>回到项目列表</Button>
|
||
]}
|
||
>
|
||
{/* 兜底页里的异常回显也是反馈表面(R19):与工作台其它提示条同一个长相 */}
|
||
<Notice danger
|
||
description={
|
||
<Typography.Text type="secondary">
|
||
兜底页由 ErrorBoundary 渲染;具体堆栈在浏览器控制台的 [nex] 页面渲染失败。
|
||
</Typography.Text>
|
||
}>
|
||
{(chunk ? '分包加载失败' : '渲染异常') + ':' + (error.message || String(error))}
|
||
</Notice>
|
||
</Result>
|
||
)
|
||
}
|
||
}
|