import React, { Component, ErrorInfo, ReactNode } from 'react'; import { AlertTriangle, Home, RefreshCw } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { handleError } from '@/lib/errorHandler'; interface RouteErrorBoundaryProps { children: ReactNode; } interface RouteErrorBoundaryState { hasError: boolean; error: Error | null; } type ErrorWithId = Error & { errorId: string }; export class RouteErrorBoundary extends Component { constructor(props: RouteErrorBoundaryProps) { super(props); this.state = { hasError: false, error: null, }; } static getDerivedStateFromError(error: Error): Partial { return { hasError: true, error, }; } componentDidCatch(error: Error, errorInfo: ErrorInfo) { // Detect chunk load failures (deployment cache issue) const isChunkLoadError = error.message.includes('Failed to fetch dynamically imported module') || error.message.includes('Loading chunk') || error.message.includes('ChunkLoadError'); if (isChunkLoadError) { // Check if we've already tried reloading const hasReloaded = sessionStorage.getItem('chunk-load-reload'); if (!hasReloaded) { // Mark as reloaded and reload once sessionStorage.setItem('chunk-load-reload', 'true'); window.location.reload(); return; // Don't log error yet } else { // Second failure - clear flag and show error sessionStorage.removeItem('chunk-load-reload'); } } // Log to database and get error ID for user reference const errorId = handleError(error, { action: 'Route-level component crash', metadata: { componentStack: errorInfo.componentStack, url: window.location.href, severity: isChunkLoadError ? 'medium' : 'critical', isChunkLoadError, }, }); this.setState({ error: { ...error, errorId } as ErrorWithId }); } handleReload = () => { window.location.reload(); }; handleGoHome = () => { window.location.href = '/'; }; render() { if (this.state.hasError) { const isChunkError = this.state.error?.message.includes('Failed to fetch dynamically imported module') || this.state.error?.message.includes('Loading chunk') || this.state.error?.message.includes('ChunkLoadError'); return (
{isChunkError ? 'New Version Available' : 'Something Went Wrong'} {isChunkError ? "The app has been updated. Please reload the page to get the latest version." : "We encountered an unexpected error. This has been logged and we'll look into it."}
{this.state.error && (
{import.meta.env.DEV && (

{this.state.error.message}

)} {(this.state.error as ErrorWithId)?.errorId && (

Reference ID: {(this.state.error as ErrorWithId).errorId.slice(0, 8)}

)}
)}

If this problem persists, please contact support

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