import { Component, ErrorInfo, ReactNode } from 'react'; import { Button } from '@/components/ui/button'; import { isChunkLoadError, reloadForStaleChunk } from '../utils/chunkLoad'; interface Props { children: ReactNode } interface State { error: Error | null; reloading: boolean } /** 捕获渲染异常;发版 chunk 失效时自动刷新 */ export default class ErrorBoundary extends Component { state: State = { error: null, reloading: false }; static getDerivedStateFromError(error: Error) { if (isChunkLoadError(error) && reloadForStaleChunk()) { return { error, reloading: true }; } return { error, reloading: false }; } componentDidCatch(error: Error, info: ErrorInfo) { console.error('[Jiang13Forum]', error, info.componentStack); } render() { if (this.state.reloading) { return (

正在更新页面

检测到程序已更新,正在自动刷新以加载最新版本…

); } if (this.state.error) { return (
500

页面渲染出错

{this.state.error.message || '发生了意外错误,请尝试刷新页面。'}

); } return this.props.children; } }