fix(worker): 修复寄卖页分片加载失败导致的白屏
- 新增全局/页面级错误边界,页面渲染异常不再整页白屏 - 监听 vite:preloadError,部署后旧分片加载失败自动刷新一次 - 修复长会话 setTimeout 2^31 溢出导致立即登出 - 寄卖页资料接口失败给出重试态,不再误报未开通
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
import { Component, type ErrorInfo, type ReactNode } from 'react'
|
||||
import { Button, Result, Space, Typography } from 'antd'
|
||||
|
||||
import { isChunkLoadError, shouldAutoReloadChunkFailure } from '@/utils/chunk-load-error'
|
||||
import { isWorkerSite } from '@/utils/site'
|
||||
|
||||
type AppErrorBoundaryProps = {
|
||||
children: ReactNode
|
||||
/** inline:只占内容区,保留侧边栏/导航;page:整页错误页。 */
|
||||
variant?: 'page' | 'inline'
|
||||
/** 路由变化时自动清除错误,避免一个页面崩溃后整个会话都停在错误页。 */
|
||||
resetKey?: string
|
||||
}
|
||||
|
||||
type AppErrorBoundaryState = {
|
||||
error: Error | null
|
||||
}
|
||||
|
||||
/**
|
||||
* 全局兜底错误边界。
|
||||
*
|
||||
* 没有它时,任意一个页面在渲染期抛错(典型场景:部署后点击尚未加载过的路由,
|
||||
* 旧的 lazy chunk 已被新镜像删除,`import()` 直接失败)会让 React 卸载整棵树,
|
||||
* 用户看到的就是没有任何提示的纯白页面。这里统一转成可操作的错误页,并对
|
||||
* 分片加载失败自动刷新一次拿新版本。
|
||||
*/
|
||||
export default class AppErrorBoundary extends Component<
|
||||
AppErrorBoundaryProps,
|
||||
AppErrorBoundaryState
|
||||
> {
|
||||
state: AppErrorBoundaryState = { error: null }
|
||||
|
||||
static getDerivedStateFromError(error: unknown): AppErrorBoundaryState {
|
||||
return { error: error instanceof Error ? error : new Error(String(error)) }
|
||||
}
|
||||
|
||||
componentDidCatch(error: unknown, info: ErrorInfo) {
|
||||
if (isChunkLoadError(error) && typeof window !== 'undefined') {
|
||||
// 旧版本页面引用了已删除的分片:刷新一次即可拿到新 index.html 与新分片。
|
||||
// 防抖窗口内再次失败则不再刷新,落到下面错误页,避免无限刷新。
|
||||
if (shouldAutoReloadChunkFailure(window.sessionStorage)) {
|
||||
window.location.reload()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
console.error('[AppErrorBoundary] 页面渲染失败', error, info.componentStack)
|
||||
}
|
||||
|
||||
componentDidUpdate(prevProps: AppErrorBoundaryProps) {
|
||||
if (this.state.error && prevProps.resetKey !== this.props.resetKey) {
|
||||
this.setState({ error: null })
|
||||
}
|
||||
}
|
||||
|
||||
render() {
|
||||
const { error } = this.state
|
||||
if (!error) return this.props.children
|
||||
|
||||
const chunkFailed = isChunkLoadError(error)
|
||||
const inline = this.props.variant === 'inline'
|
||||
const homePath = isWorkerSite() ? '/hall' : '/'
|
||||
|
||||
return (
|
||||
<div
|
||||
data-testid="app-error-boundary"
|
||||
style={
|
||||
inline
|
||||
? { padding: '32px 16px' }
|
||||
: {
|
||||
minHeight: '100vh',
|
||||
display: 'flex',
|
||||
alignItems: 'center',
|
||||
justifyContent: 'center',
|
||||
padding: 24,
|
||||
background: '#f5f7fb',
|
||||
}
|
||||
}
|
||||
>
|
||||
<Result
|
||||
status="error"
|
||||
title={chunkFailed ? '页面资源已更新' : '页面加载失败'}
|
||||
subTitle={
|
||||
chunkFailed
|
||||
? '检测到页面版本已更新或网络中断,刷新后即可继续使用。'
|
||||
: '页面遇到异常,已停止渲染以免影响后续操作。'
|
||||
}
|
||||
extra={
|
||||
<Space direction="vertical" size={12} align="center">
|
||||
<Space wrap>
|
||||
<Button type="primary" onClick={() => window.location.reload()}>
|
||||
重新加载
|
||||
</Button>
|
||||
<Button onClick={() => window.location.assign(homePath)}>返回首页</Button>
|
||||
</Space>
|
||||
{chunkFailed ? null : (
|
||||
<Typography.Text type="secondary" style={{ fontSize: 12, maxWidth: 520 }}>
|
||||
错误信息:{error.message.slice(0, 200)}
|
||||
</Typography.Text>
|
||||
)}
|
||||
</Space>
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user