fix(router): add ErrorBoundary and lazyWithRetry to prevent dynamic import chunk failures
Deploy Canina / deploy (push) Successful in 31s
Deploy Canina / deploy (push) Successful in 31s
This commit is contained in:
1 parent
f430c4931f
commit
f306b95f62
14 files changed
+48329
-47545
No files matched your search
@@ -0,0 +1,111 @@
|
||||
import { Component, ReactNode, ErrorInfo } from 'react';
|
||||
import { AlertCircle, RefreshCw, Home } from 'lucide-react';
|
||||
|
||||
interface Props {
|
||||
children: ReactNode;
|
||||
fallback?: ReactNode;
|
||||
}
|
||||
|
||||
interface State {
|
||||
hasError: boolean;
|
||||
error: Error | null;
|
||||
isChunkLoadError: boolean;
|
||||
}
|
||||
|
||||
export class RouteErrorBoundary extends Component<Props, State> {
|
||||
public state: State = {
|
||||
hasError: false,
|
||||
error: null,
|
||||
isChunkLoadError: false,
|
||||
};
|
||||
|
||||
public static getDerivedStateFromError(error: Error): State {
|
||||
const isChunk =
|
||||
error.name === 'ChunkLoadError' ||
|
||||
/Failed to fetch dynamically imported module/i.test(error.message) ||
|
||||
/Loading chunk .* failed/i.test(error.message) ||
|
||||
/dynamically imported module/i.test(error.message);
|
||||
|
||||
return {
|
||||
hasError: true,
|
||||
error,
|
||||
isChunkLoadError: isChunk,
|
||||
};
|
||||
}
|
||||
|
||||
public componentDidCatch(error: Error, errorInfo: ErrorInfo) {
|
||||
console.error('[RouteErrorBoundary] Uncaught error:', error, errorInfo);
|
||||
|
||||
// Auto-reload on chunk mismatch once to seamlessly fetch updated assets from server
|
||||
if (this.state.isChunkLoadError) {
|
||||
const storageKey = 'canina_chunk_reload_ts';
|
||||
const lastReload = sessionStorage.getItem(storageKey);
|
||||
const now = Date.now();
|
||||
|
||||
// Only auto-reload if we haven't done so in the last 10 seconds
|
||||
if (!lastReload || now - parseInt(lastReload, 10) > 10000) {
|
||||
sessionStorage.setItem(storageKey, String(now));
|
||||
window.location.reload();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private handleReload = () => {
|
||||
window.location.reload();
|
||||
};
|
||||
|
||||
private handleGoHome = () => {
|
||||
window.location.href = '/';
|
||||
};
|
||||
|
||||
public render() {
|
||||
if (this.state.hasError) {
|
||||
if (this.props.fallback) {
|
||||
return this.props.fallback;
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="min-h-[400px] flex items-center justify-center p-6 font-vazir" dir="rtl">
|
||||
<div className="bg-white p-8 rounded-3xl shadow-xl border border-gray-200 max-w-md w-full text-center space-y-5">
|
||||
<div className="w-16 h-16 bg-amber-50 text-amber-600 rounded-3xl flex items-center justify-center mx-auto border border-amber-200">
|
||||
<AlertCircle className="w-8 h-8" />
|
||||
</div>
|
||||
|
||||
<div className="space-y-1.5">
|
||||
<h3 className="text-lg font-black text-gray-900">
|
||||
{this.state.isChunkLoadError
|
||||
? 'نسخه جدیدی از پنل ادمین منتشر شده است'
|
||||
: 'خطا در بارگذاری بخش مورد نظر'}
|
||||
</h3>
|
||||
<p className="text-xs text-gray-500 font-medium leading-relaxed">
|
||||
{this.state.isChunkLoadError
|
||||
? 'فایلهای جاوااسکریپت این بخش روی سرور بهروزرسانی شدهاند. برای بارگذاری نسخه جدید دکمه بروزرسانی را بزنید.'
|
||||
: this.state.error?.message || 'یک خطای غیرمنتظره در بارگذاری صفحه رخ داد.'}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center justify-center gap-3 pt-2">
|
||||
<button
|
||||
onClick={this.handleReload}
|
||||
className="bg-purple-600 hover:bg-purple-700 text-white text-xs font-bold py-2.5 px-5 rounded-xl transition-all flex items-center gap-2 shadow-md shadow-purple-200 cursor-pointer"
|
||||
>
|
||||
<RefreshCw className="w-4 h-4" />
|
||||
<span>بروزرسانی صفحه</span>
|
||||
</button>
|
||||
|
||||
<button
|
||||
onClick={this.handleGoHome}
|
||||
className="bg-gray-100 hover:bg-gray-200 text-gray-700 text-xs font-bold py-2.5 px-4 rounded-xl transition-all flex items-center gap-1.5 cursor-pointer"
|
||||
>
|
||||
<Home className="w-4 h-4" />
|
||||
<span>داشبورد</span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return this.props.children;
|
||||
}
|
||||
}
|
||||
@@ -1,44 +1,44 @@
|
||||
/* eslint-disable react-refresh/only-export-components */
|
||||
import { lazy } from 'react';
|
||||
import type { ComponentType } from 'react';
|
||||
import { createBrowserRouter, Navigate } from 'react-router-dom';
|
||||
import Layout from '../components/Layout';
|
||||
import ProtectedRoute from '../components/ProtectedRoute';
|
||||
import Login from '../pages/Login';
|
||||
import { RouteErrorBoundary } from '../components/RouteErrorBoundary';
|
||||
import { lazyWithRetry } from '../utils/lazyWithRetry';
|
||||
|
||||
// Lazy loading admin pages
|
||||
const Dashboard = lazy(() => import('../pages/Dashboard'));
|
||||
const Users = lazy(() => import('../pages/Users'));
|
||||
const Products = lazy(() => import('../pages/Products'));
|
||||
const Orders = lazy(() => import('../pages/Orders'));
|
||||
const Coupons = lazy(() => import('../pages/Coupons'));
|
||||
const Settings = lazy(() => import('../pages/Settings'));
|
||||
const Reports = lazy(() => import('../pages/Reports'));
|
||||
const Categories = lazy(() => import('../pages/Categories'));
|
||||
const Blogs = lazy(() => import('../pages/Blogs'));
|
||||
const Wiki = lazy(() => import('../pages/Wiki'));
|
||||
const Pets = lazy(() => import('../pages/Pets'));
|
||||
const UITexts = lazy(() => import('../pages/UITexts'));
|
||||
const Media = lazy(() => import('../pages/Media'));
|
||||
const CMS = lazy(() => import('../pages/CMS'));
|
||||
const WholesaleApplications = lazy(() => import('../pages/WholesaleApplications'));
|
||||
const Videos = lazy(() => import('../pages/Videos'));
|
||||
const ContactSubmissions = lazy(() => import('../pages/ContactSubmissions'));
|
||||
const BannersManager = lazy(() => import('../pages/BannersManager'));
|
||||
const SmartAdvisorManager = lazy(() => import('../pages/SmartAdvisorManager'));
|
||||
const TestimonialsManager = lazy(() => import('../pages/TestimonialsManager'));
|
||||
const IngredientsManager = lazy(() => import('../pages/IngredientsManager'));
|
||||
const PrescriptionsManager = lazy(() => import('../pages/PrescriptionsManager'));
|
||||
const B2BManager = lazy(() => import('../pages/B2BManager'));
|
||||
const SeoSettingsPage = lazy(() => import('../pages/SeoSettingsPage'));
|
||||
const FinancialSettingsPage = lazy(() => import('../pages/FinancialSettingsPage'));
|
||||
const SystemSettingsPage = lazy(() => import('../pages/SystemSettingsPage'));
|
||||
const SmsSettingsPage = lazy(() => import('../pages/SmsSettingsPage'));
|
||||
const SslSettingsPage = lazy(() => import('../pages/SslSettingsPage'));
|
||||
const Transactions = lazy(() => import('../pages/Transactions'));
|
||||
const Tickets = lazy(() => import('../pages/Tickets'));
|
||||
const Reviews = lazy(() => import('../pages/Reviews'));
|
||||
const DoctorsManager = lazy(() => import('../pages/DoctorsManager'));
|
||||
// Lazy loading admin pages with automatic retry and cache recovery
|
||||
const Dashboard = lazyWithRetry(() => import('../pages/Dashboard'));
|
||||
const Users = lazyWithRetry(() => import('../pages/Users'));
|
||||
const Products = lazyWithRetry(() => import('../pages/Products'));
|
||||
const Orders = lazyWithRetry(() => import('../pages/Orders'));
|
||||
const Coupons = lazyWithRetry(() => import('../pages/Coupons'));
|
||||
const Settings = lazyWithRetry(() => import('../pages/Settings'));
|
||||
const Reports = lazyWithRetry(() => import('../pages/Reports'));
|
||||
const Categories = lazyWithRetry(() => import('../pages/Categories'));
|
||||
const Blogs = lazyWithRetry(() => import('../pages/Blogs'));
|
||||
const Wiki = lazyWithRetry(() => import('../pages/Wiki'));
|
||||
const Pets = lazyWithRetry(() => import('../pages/Pets'));
|
||||
const UITexts = lazyWithRetry(() => import('../pages/UITexts'));
|
||||
const Media = lazyWithRetry(() => import('../pages/Media'));
|
||||
const CMS = lazyWithRetry(() => import('../pages/CMS'));
|
||||
const WholesaleApplications = lazyWithRetry(() => import('../pages/WholesaleApplications'));
|
||||
const Videos = lazyWithRetry(() => import('../pages/Videos'));
|
||||
const ContactSubmissions = lazyWithRetry(() => import('../pages/ContactSubmissions'));
|
||||
const BannersManager = lazyWithRetry(() => import('../pages/BannersManager'));
|
||||
const SmartAdvisorManager = lazyWithRetry(() => import('../pages/SmartAdvisorManager'));
|
||||
const TestimonialsManager = lazyWithRetry(() => import('../pages/TestimonialsManager'));
|
||||
const IngredientsManager = lazyWithRetry(() => import('../pages/IngredientsManager'));
|
||||
const PrescriptionsManager = lazyWithRetry(() => import('../pages/PrescriptionsManager'));
|
||||
const B2BManager = lazyWithRetry(() => import('../pages/B2BManager'));
|
||||
const SeoSettingsPage = lazyWithRetry(() => import('../pages/SeoSettingsPage'));
|
||||
const FinancialSettingsPage = lazyWithRetry(() => import('../pages/FinancialSettingsPage'));
|
||||
const SystemSettingsPage = lazyWithRetry(() => import('../pages/SystemSettingsPage'));
|
||||
const SmsSettingsPage = lazyWithRetry(() => import('../pages/SmsSettingsPage'));
|
||||
const SslSettingsPage = lazyWithRetry(() => import('../pages/SslSettingsPage'));
|
||||
const Transactions = lazyWithRetry(() => import('../pages/Transactions'));
|
||||
const Tickets = lazyWithRetry(() => import('../pages/Tickets'));
|
||||
const Reviews = lazyWithRetry(() => import('../pages/Reviews'));
|
||||
const DoctorsManager = lazyWithRetry(() => import('../pages/DoctorsManager'));
|
||||
|
||||
export interface AdminRouteConfig {
|
||||
path: string;
|
||||
@@ -50,13 +50,16 @@ export const router = createBrowserRouter([
|
||||
{
|
||||
path: '/login',
|
||||
element: <Login />,
|
||||
errorElement: <RouteErrorBoundary><Login /></RouteErrorBoundary>,
|
||||
},
|
||||
{
|
||||
element: <ProtectedRoute />,
|
||||
errorElement: <RouteErrorBoundary><Layout /></RouteErrorBoundary>,
|
||||
children: [
|
||||
{
|
||||
path: '/',
|
||||
element: <Layout />,
|
||||
errorElement: <RouteErrorBoundary><Layout /></RouteErrorBoundary>,
|
||||
children: [
|
||||
{ index: true, element: <Dashboard /> },
|
||||
{ path: 'users/*', element: <Users /> },
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
import { ComponentType, lazy } from 'react';
|
||||
|
||||
/**
|
||||
* Wraps dynamic React.lazy imports with retry logic and auto cache-busting
|
||||
* to prevent "Failed to fetch dynamically imported module" errors after deployments.
|
||||
*/
|
||||
export function lazyWithRetry<T extends ComponentType<unknown>>(
|
||||
componentImport: () => Promise<{ default: T }>,
|
||||
retriesLeft = 2,
|
||||
interval = 1000
|
||||
): React.LazyExoticComponent<T> {
|
||||
return lazy(() =>
|
||||
new Promise<{ default: T }>((resolve, reject) => {
|
||||
componentImport()
|
||||
.then(resolve)
|
||||
.catch((error: unknown) => {
|
||||
if (retriesLeft <= 0) {
|
||||
// Check if page was already reloaded for chunk error
|
||||
const hasReloaded = sessionStorage.getItem('chunk_retry_reloaded');
|
||||
if (!hasReloaded) {
|
||||
sessionStorage.setItem('chunk_retry_reloaded', 'true');
|
||||
window.location.reload();
|
||||
return;
|
||||
}
|
||||
reject(error);
|
||||
return;
|
||||
}
|
||||
|
||||
setTimeout(() => {
|
||||
lazyWithRetry(componentImport, retriesLeft - 1, interval);
|
||||
}, interval);
|
||||
});
|
||||
})
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user