|
| 1 | +import {ErrorInfo} from 'react'; |
| 2 | +import * as React from 'react'; |
| 3 | + |
| 4 | +interface State { |
| 5 | + error?: Error & {response?: any}; |
| 6 | + errorInfo?: ErrorInfo; |
| 7 | + isReloading: boolean; |
| 8 | +} |
| 9 | + |
| 10 | +/** |
| 11 | + * Error boundary specifically for handling chunk loading failures. |
| 12 | + * Automatically reloads the page when a chunk fails to load. |
| 13 | + * Fixes: https://github.com/argoproj/argo-workflows/issues/15640 |
| 14 | + */ |
| 15 | +export class ChunkLoadErrorBoundary extends React.Component<any, State> { |
| 16 | + static isChunkLoadError(error: Error): boolean { |
| 17 | + return ( |
| 18 | + error.message.includes('Loading chunk') || |
| 19 | + error.message.includes('Failed to fetch') || |
| 20 | + error.message.includes('Failed to import') || |
| 21 | + error.message.includes('NetworkError') || |
| 22 | + error.name === 'ChunkLoadError' |
| 23 | + ); |
| 24 | + } |
| 25 | + |
| 26 | + static getDerivedStateFromError(error: Error) { |
| 27 | + // Only handle chunk load errors; re-throw others |
| 28 | + if (ChunkLoadErrorBoundary.isChunkLoadError(error)) { |
| 29 | + return {error, isReloading: true}; |
| 30 | + } |
| 31 | + throw error; |
| 32 | + } |
| 33 | + |
| 34 | + constructor(props: any) { |
| 35 | + super(props); |
| 36 | + this.state = {isReloading: false}; |
| 37 | + } |
| 38 | + |
| 39 | + componentDidCatch(error: Error, errorInfo: ErrorInfo) { |
| 40 | + console.error('Chunk load error:', error, errorInfo); |
| 41 | + // Auto-reload after a brief delay to allow state update |
| 42 | + setTimeout(() => window.location.reload(), 100); |
| 43 | + } |
| 44 | + |
| 45 | + render() { |
| 46 | + if (this.state.isReloading) { |
| 47 | + return ( |
| 48 | + <div style={{padding: '20px', textAlign: 'center'}}> |
| 49 | + <h2>Reloading...</h2> |
| 50 | + <p>A required component failed to load. The page will reload automatically.</p> |
| 51 | + </div> |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + return this.props.children; |
| 56 | + } |
| 57 | +} |
0 commit comments