|
| 1 | +/* eslint-disable i18next/no-literal-string -- This boundary is the last line of |
| 2 | + defense for a broken boot; it must not depend on i18n (which may be exactly |
| 3 | + what failed to load), so its copy is intentionally hard-coded English. */ |
| 4 | +import { Component, ErrorInfo, ReactNode, CSSProperties } from 'react'; |
| 5 | +import { reloadOnAppEntrypointHeadersMismatch } from './checkAppEntrypointHeadersMatch'; |
| 6 | +import errorReporting from 'ErrorReporting'; |
| 7 | + |
| 8 | +// Guard against an auto-reload loop: if we reloaded very recently, show the |
| 9 | +// manual fallback instead of reloading again. |
| 10 | +const RELOAD_GUARD_KEY = 'intercode.bootErrorReloadAt'; |
| 11 | +const RELOAD_GUARD_WINDOW_MS = 15_000; |
| 12 | + |
| 13 | +function reloadedWithinGuardWindow(): boolean { |
| 14 | + try { |
| 15 | + const last = Number(sessionStorage.getItem(RELOAD_GUARD_KEY)); |
| 16 | + return Number.isFinite(last) && last > 0 && Date.now() - last < RELOAD_GUARD_WINDOW_MS; |
| 17 | + } catch { |
| 18 | + return false; |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +function markReloadAttempt(): void { |
| 23 | + try { |
| 24 | + sessionStorage.setItem(RELOAD_GUARD_KEY, String(Date.now())); |
| 25 | + } catch { |
| 26 | + // sessionStorage may be unavailable (private mode / blocked); best effort. |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +const containerStyle: CSSProperties = { |
| 31 | + maxWidth: '32rem', |
| 32 | + margin: '4rem auto', |
| 33 | + padding: '0 1rem', |
| 34 | + fontFamily: 'system-ui, sans-serif', |
| 35 | + textAlign: 'center', |
| 36 | + lineHeight: 1.5, |
| 37 | +}; |
| 38 | + |
| 39 | +const buttonStyle: CSSProperties = { |
| 40 | + marginTop: '1rem', |
| 41 | + padding: '0.5rem 1.25rem', |
| 42 | + fontSize: '1rem', |
| 43 | + cursor: 'pointer', |
| 44 | +}; |
| 45 | + |
| 46 | +function BootErrorFallback() { |
| 47 | + return ( |
| 48 | + <div style={containerStyle} role="alert"> |
| 49 | + <h1 style={{ fontSize: '1.25rem' }}>We couldn’t finish loading this page</h1> |
| 50 | + <p>This is usually temporary and fixed by reloading. If it keeps happening, try again in a few minutes.</p> |
| 51 | + <button type="button" style={buttonStyle} onClick={() => window.location.reload()}> |
| 52 | + Reload the page |
| 53 | + </button> |
| 54 | + </div> |
| 55 | + ); |
| 56 | +} |
| 57 | + |
| 58 | +type Props = { children: ReactNode }; |
| 59 | +type State = { hasError: boolean }; |
| 60 | + |
| 61 | +// Catches failures during the SPA's initial boot/render — a rejected |
| 62 | +// bootstrapPromise, or an uncaught error rendering the route tree — that would |
| 63 | +// otherwise leave a blank white page with no recovery. A stale deploy (a chunk |
| 64 | +// hash that no longer exists) is the most common cause, so we try to reload |
| 65 | +// onto the fresh bundle; anything else gets a visible, reportable error instead |
| 66 | +// of a blank screen. |
| 67 | +export default class BootErrorBoundary extends Component<Props, State> { |
| 68 | + state: State = { hasError: false }; |
| 69 | + |
| 70 | + static getDerivedStateFromError(): State { |
| 71 | + return { hasError: true }; |
| 72 | + } |
| 73 | + |
| 74 | + componentDidCatch(error: Error, info: ErrorInfo): void { |
| 75 | + try { |
| 76 | + errorReporting().error(error, { tags: { context: 'boot' }, componentStack: info.componentStack }); |
| 77 | + } catch { |
| 78 | + // Error reporting itself may be unavailable (e.g. its SDK was the chunk |
| 79 | + // that failed to load); don't let that mask the fallback UI. |
| 80 | + } |
| 81 | + |
| 82 | + // Very often a blank boot is a stale deploy. If the deployed entrypoint has |
| 83 | + // changed under this tab, reload to pick up the fresh bundle — but only if |
| 84 | + // we didn't just try, so we never get stuck in a reload loop. |
| 85 | + if (!reloadedWithinGuardWindow()) { |
| 86 | + markReloadAttempt(); |
| 87 | + void reloadOnAppEntrypointHeadersMismatch(); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + render(): ReactNode { |
| 92 | + if (this.state.hasError) { |
| 93 | + return <BootErrorFallback />; |
| 94 | + } |
| 95 | + return this.props.children; |
| 96 | + } |
| 97 | +} |
0 commit comments