|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { Component, ReactNode } from 'react'; |
| 4 | + |
| 5 | +interface Props { |
| 6 | + children: ReactNode; |
| 7 | +} |
| 8 | + |
| 9 | +interface State { |
| 10 | + hasError: boolean; |
| 11 | + error?: Error; |
| 12 | +} |
| 13 | + |
| 14 | +/** |
| 15 | + * Error boundary to handle ChunkLoadError |
| 16 | + * Automatically reloads the page once when chunk loading fails |
| 17 | + * This typically happens after deployments when cached HTML |
| 18 | + * tries to load new chunk files that have different hashes |
| 19 | + */ |
| 20 | +export class ChunkLoadErrorBoundary extends Component<Props, State> { |
| 21 | + constructor(props: Props) { |
| 22 | + super(props); |
| 23 | + this.state = { hasError: false }; |
| 24 | + } |
| 25 | + |
| 26 | + componentDidMount() { |
| 27 | + if (typeof window === 'undefined') { |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + // Only clear the reload marker when we reach a clean render |
| 32 | + if (!this.state.hasError && sessionStorage.getItem('chunk-load-error-reloaded')) { |
| 33 | + sessionStorage.removeItem('chunk-load-error-reloaded'); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + private handleManualReload = () => { |
| 38 | + if (typeof window === 'undefined') { |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + sessionStorage.removeItem('chunk-load-error-reloaded'); |
| 43 | + window.location.reload(); |
| 44 | + }; |
| 45 | + |
| 46 | + static getDerivedStateFromError(error: Error): State { |
| 47 | + // Check if it's a chunk loading error |
| 48 | + const isChunkLoadError = |
| 49 | + error.name === 'ChunkLoadError' || |
| 50 | + error.message.includes('Loading chunk') || |
| 51 | + error.message.includes('ChunkLoadError'); |
| 52 | + |
| 53 | + return { hasError: isChunkLoadError, error }; |
| 54 | + } |
| 55 | + |
| 56 | + componentDidCatch(error: Error) { |
| 57 | + // Only handle chunk load errors |
| 58 | + const isChunkLoadError = |
| 59 | + error.name === 'ChunkLoadError' || |
| 60 | + error.message.includes('Loading chunk') || |
| 61 | + error.message.includes('ChunkLoadError'); |
| 62 | + |
| 63 | + if (isChunkLoadError) { |
| 64 | + console.warn('[ChunkLoadError] Detected chunk loading failure, reloading page...'); |
| 65 | + |
| 66 | + // Check if we've already reloaded (prevent infinite loops) |
| 67 | + const hasReloaded = sessionStorage.getItem('chunk-load-error-reloaded'); |
| 68 | + |
| 69 | + if (!hasReloaded) { |
| 70 | + // Mark that we're reloading |
| 71 | + sessionStorage.setItem('chunk-load-error-reloaded', 'true'); |
| 72 | + |
| 73 | + // Reload the page to get fresh HTML and chunks |
| 74 | + window.location.reload(); |
| 75 | + } else { |
| 76 | + // Already reloaded once, don't loop |
| 77 | + console.error('[ChunkLoadError] Already reloaded once, not reloading again'); |
| 78 | + sessionStorage.removeItem('chunk-load-error-reloaded'); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + render() { |
| 84 | + if (this.state.hasError) { |
| 85 | + // Show a brief loading message while reloading |
| 86 | + return ( |
| 87 | + <div |
| 88 | + style={{ |
| 89 | + display: 'flex', |
| 90 | + alignItems: 'center', |
| 91 | + justifyContent: 'center', |
| 92 | + minHeight: '100vh', |
| 93 | + flexDirection: 'column', |
| 94 | + gap: '16px', |
| 95 | + padding: '20px', |
| 96 | + textAlign: 'center', |
| 97 | + }} |
| 98 | + > |
| 99 | + <div style={{ fontSize: '18px', fontWeight: 500 }}>Updating...</div> |
| 100 | + <div style={{ fontSize: '14px', color: '#666' }}> |
| 101 | + Loading the latest version |
| 102 | + </div> |
| 103 | + <button |
| 104 | + onClick={this.handleManualReload} |
| 105 | + style={{ |
| 106 | + padding: '10px 16px', |
| 107 | + borderRadius: '8px', |
| 108 | + border: '1px solid #ccc', |
| 109 | + background: '#fff', |
| 110 | + fontSize: '14px', |
| 111 | + cursor: 'pointer', |
| 112 | + }} |
| 113 | + > |
| 114 | + Reload now |
| 115 | + </button> |
| 116 | + </div> |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + return this.props.children; |
| 121 | + } |
| 122 | +} |
0 commit comments