347 lines
12 KiB
TypeScript
347 lines
12 KiB
TypeScript
/**
|
|
* Shared lazy page loaders + intentional prefetch (hover / idle).
|
|
* First navigation to a route otherwise pays for the network + parse of its chunk.
|
|
*/
|
|
import { lazy, type ComponentType, type LazyExoticComponent } from 'react';
|
|
|
|
type AnyModule = Record<string, unknown>;
|
|
|
|
type LazyPage = LazyExoticComponent<ComponentType<Record<string, never>>> & {
|
|
preload: () => Promise<unknown>;
|
|
};
|
|
|
|
const preloadCache = new Map<string, Promise<unknown>>();
|
|
|
|
function once(key: string, load: () => Promise<unknown>): Promise<unknown> {
|
|
let pending = preloadCache.get(key);
|
|
if (!pending) {
|
|
pending = load().catch((err) => {
|
|
preloadCache.delete(key);
|
|
throw err;
|
|
});
|
|
preloadCache.set(key, pending);
|
|
}
|
|
return pending;
|
|
}
|
|
|
|
const CHUNK_RELOAD_KEY = 'tl_chunk_reload_once';
|
|
|
|
function isChunkLoadFailure(err: unknown): boolean {
|
|
const msg = err instanceof Error ? err.message : String(err ?? '');
|
|
return /Failed to fetch dynamically imported module|Loading chunk [\d]+ failed|ChunkLoadError|Importing a module script failed/i.test(
|
|
msg
|
|
);
|
|
}
|
|
|
|
async function loadWithChunkRecovery<T>(_key: string, loader: () => Promise<T>): Promise<T> {
|
|
try {
|
|
const mod = await loader();
|
|
try {
|
|
sessionStorage.removeItem(CHUNK_RELOAD_KEY);
|
|
} catch {
|
|
/* ignore */
|
|
}
|
|
return mod;
|
|
} catch (err) {
|
|
if (isChunkLoadFailure(err)) {
|
|
try {
|
|
if (!sessionStorage.getItem(CHUNK_RELOAD_KEY)) {
|
|
sessionStorage.setItem(CHUNK_RELOAD_KEY, '1');
|
|
window.location.reload();
|
|
// Stay pending until the page unloads.
|
|
return await new Promise<T>(() => undefined);
|
|
}
|
|
sessionStorage.removeItem(CHUNK_RELOAD_KEY);
|
|
} catch {
|
|
/* ignore storage / reload issues */
|
|
}
|
|
}
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
function lazyNamed<T extends AnyModule, K extends keyof T>(
|
|
key: string,
|
|
loader: () => Promise<T>,
|
|
exportName: K
|
|
): LazyPage {
|
|
const safeLoader = () => loadWithChunkRecovery(key, loader);
|
|
|
|
const preload = () =>
|
|
once(key, async () => {
|
|
const mod = await safeLoader();
|
|
return mod[exportName];
|
|
});
|
|
|
|
const Comp = lazy(async () => {
|
|
const mod = await safeLoader();
|
|
// Keep cache warm for subsequent prefetchRoute calls.
|
|
void once(key, async () => mod[exportName]);
|
|
return { default: mod[exportName] as ComponentType<Record<string, never>> };
|
|
}) as LazyPage;
|
|
|
|
Comp.preload = preload;
|
|
return Comp;
|
|
}
|
|
|
|
export const RegisterPage = lazyNamed('RegisterPage', () => import('../../features/auth/RegisterPage'), 'RegisterPage');
|
|
export const VerifyEmailPage = lazyNamed(
|
|
'VerifyEmailPage',
|
|
() => import('../../features/auth/VerifyEmailPage'),
|
|
'VerifyEmailPage'
|
|
);
|
|
export const ForgotPasswordPage = lazyNamed(
|
|
'ForgotPasswordPage',
|
|
() => import('../../features/auth/ForgotPasswordPage'),
|
|
'ForgotPasswordPage'
|
|
);
|
|
export const ResetPasswordPage = lazyNamed(
|
|
'ResetPasswordPage',
|
|
() => import('../../features/auth/ResetPasswordPage'),
|
|
'ResetPasswordPage'
|
|
);
|
|
export const FaqPublicPage = lazyNamed(
|
|
'FaqPublicPage',
|
|
() => import('../../features/faq/FaqPublicPage'),
|
|
'FaqPublicPage'
|
|
);
|
|
export const LandingEntry = lazyNamed(
|
|
'LandingEntry',
|
|
() => import('../../features/landing/LandingEntry'),
|
|
'LandingEntry'
|
|
);
|
|
export const ShareResultVerifyPage = lazyNamed(
|
|
'ShareResultVerifyPage',
|
|
() => import('../../features/share-cards/ShareResultVerifyPage'),
|
|
'ShareResultVerifyPage'
|
|
);
|
|
|
|
export const HomePage = lazyNamed('HomePage', () => import('../../features/home/HomePage'), 'HomePage');
|
|
export const GamePlayRoute = lazyNamed(
|
|
'GamePlayRoute',
|
|
() => import('../../features/games/GamePlayRoute'),
|
|
'GamePlayRoute'
|
|
);
|
|
export const WalletPage = lazyNamed('WalletPage', () => import('../../features/wallet/WalletPage'), 'WalletPage');
|
|
export const ReferralPage = lazyNamed(
|
|
'ReferralPage',
|
|
() => import('../../features/referral/ReferralPage'),
|
|
'ReferralPage'
|
|
);
|
|
export const ProfilePage = lazyNamed(
|
|
'ProfilePage',
|
|
() => import('../../features/profile/ProfilePage'),
|
|
'ProfilePage'
|
|
);
|
|
export const FaqPage = lazyNamed('FaqPage', () => import('../../features/faq/FaqPage'), 'FaqPage');
|
|
|
|
export const SupportAdminPage = lazyNamed(
|
|
'SupportAdminPage',
|
|
() => import('../../features/support/SupportAdminPage'),
|
|
'SupportAdminPage'
|
|
);
|
|
export const FaqAdminPage = lazyNamed(
|
|
'FaqAdminPage',
|
|
() => import('../../features/faq/FaqAdminPage'),
|
|
'FaqAdminPage'
|
|
);
|
|
|
|
export const DashboardPage = lazyNamed(
|
|
'DashboardPage',
|
|
() => import('../../features/dashboard/DashboardPage'),
|
|
'DashboardPage'
|
|
);
|
|
export const OptionsPage = lazyNamed(
|
|
'OptionsPage',
|
|
() => import('../../features/options/OptionsPage'),
|
|
'OptionsPage'
|
|
);
|
|
export const UserManagementPage = lazyNamed(
|
|
'UserManagementPage',
|
|
() => import('../../features/users/UserManagementPage'),
|
|
'UserManagementPage'
|
|
);
|
|
export const ReportsHubPage = lazyNamed(
|
|
'ReportsHubPage',
|
|
() => import('../../features/reports/ReportsHubPage'),
|
|
'ReportsHubPage'
|
|
);
|
|
export const ReportDetailPage = lazyNamed(
|
|
'ReportDetailPage',
|
|
() => import('../../features/reports/ReportDetailPage'),
|
|
'ReportDetailPage'
|
|
);
|
|
export const SettingsPage = lazyNamed(
|
|
'SettingsPage',
|
|
() => import('../../features/settings/SettingsPage'),
|
|
'SettingsPage'
|
|
);
|
|
export const CatalogAdminPage = lazyNamed(
|
|
'CatalogAdminPage',
|
|
() => import('../../features/catalog/CatalogAdminPage'),
|
|
'CatalogAdminPage'
|
|
);
|
|
export const TreasuryAdminPage = lazyNamed(
|
|
'TreasuryAdminPage',
|
|
() => import('../../features/treasury/TreasuryAdminPage'),
|
|
'TreasuryAdminPage'
|
|
);
|
|
export const ApprovalsPage = lazyNamed(
|
|
'ApprovalsPage',
|
|
() => import('../../features/approvals/ApprovalsPage'),
|
|
'ApprovalsPage'
|
|
);
|
|
export const WithdrawalsAdminPage = lazyNamed(
|
|
'WithdrawalsAdminPage',
|
|
() => import('../../features/withdrawals/WithdrawalsAdminPage'),
|
|
'WithdrawalsAdminPage'
|
|
);
|
|
export const ApprovalPoliciesPage = lazyNamed(
|
|
'ApprovalPoliciesPage',
|
|
() => import('../../features/approvals/ApprovalPoliciesPage'),
|
|
'ApprovalPoliciesPage'
|
|
);
|
|
export const SecurityAdminPage = lazyNamed(
|
|
'SecurityAdminPage',
|
|
() => import('../../features/security/SecurityAdminPage'),
|
|
'SecurityAdminPage'
|
|
);
|
|
export const ReferralAdminPage = lazyNamed(
|
|
'ReferralAdminPage',
|
|
() => import('../../features/referral/ReferralAdminPage'),
|
|
'ReferralAdminPage'
|
|
);
|
|
export const ReferralRegularizationPage = lazyNamed(
|
|
'ReferralRegularizationPage',
|
|
() => import('../../features/referral/ReferralRegularizationPage'),
|
|
'ReferralRegularizationPage'
|
|
);
|
|
export const ReferralFraudDashboardPage = lazyNamed(
|
|
'ReferralFraudDashboardPage',
|
|
() => import('../../features/referral/ReferralFraudDashboardPage'),
|
|
'ReferralFraudDashboardPage'
|
|
);
|
|
export const TestWalletAdminPage = lazyNamed(
|
|
'TestWalletAdminPage',
|
|
() => import('../../features/test-wallet/TestWalletAdminPage'),
|
|
'TestWalletAdminPage'
|
|
);
|
|
export const ShareCardsAdminPage = lazyNamed(
|
|
'ShareCardsAdminPage',
|
|
() => import('../../features/share-cards/ShareCardsAdminPage'),
|
|
'ShareCardsAdminPage'
|
|
);
|
|
export const SoloEncouragementAdminPage = lazyNamed(
|
|
'SoloEncouragementAdminPage',
|
|
() => import('../../features/solo-encouragement/SoloEncouragementAdminPage'),
|
|
'SoloEncouragementAdminPage'
|
|
);
|
|
export const MarketingAdminPage = lazyNamed(
|
|
'MarketingAdminPage',
|
|
() => import('../../features/marketing/MarketingAdminPage'),
|
|
'MarketingAdminPage'
|
|
);
|
|
export const CountriesAdminPage = lazyNamed(
|
|
'CountriesAdminPage',
|
|
() => import('../../features/countries/CountriesAdminPage'),
|
|
'CountriesAdminPage'
|
|
);
|
|
export const NetworkStatusPage = lazyNamed(
|
|
'NetworkStatusPage',
|
|
() => import('../../features/network/NetworkStatusPage'),
|
|
'NetworkStatusPage'
|
|
);
|
|
export const RoleManagementPage = lazyNamed(
|
|
'RoleManagementPage',
|
|
() => import('../../features/rbac/RoleManagementPage'),
|
|
'RoleManagementPage'
|
|
);
|
|
export const AdminUsersPage = lazyNamed(
|
|
'AdminUsersPage',
|
|
() => import('../../features/rbac/AdminUsersPage'),
|
|
'AdminUsersPage'
|
|
);
|
|
export const AdminUserPermissionsPage = lazyNamed(
|
|
'AdminUserPermissionsPage',
|
|
() => import('../../features/rbac/AdminUserPermissionsPage'),
|
|
'AdminUserPermissionsPage'
|
|
);
|
|
export const MobileVersionsAdminPage = lazyNamed(
|
|
'MobileVersionsAdminPage',
|
|
() => import('../../features/mobile-versions/MobileVersionsAdminPage'),
|
|
'MobileVersionsAdminPage'
|
|
);
|
|
export const SslCertificatesAdminPage = lazyNamed(
|
|
'SslCertificatesAdminPage',
|
|
() => import('../../features/ssl-certificates/SslCertificatesAdminPage'),
|
|
'SslCertificatesAdminPage'
|
|
);
|
|
export const VpsMonitorAdminPage = lazyNamed(
|
|
'VpsMonitorAdminPage',
|
|
() => import('../../features/vps-monitor/VpsMonitorAdminPage'),
|
|
'VpsMonitorAdminPage'
|
|
);
|
|
|
|
/** Exact path → page preload. Longest match wins for nested paths. */
|
|
const PATH_PRELOAD: Array<{ path: string; preload: () => Promise<unknown> }> = [
|
|
{ path: '/register', preload: RegisterPage.preload },
|
|
{ path: '/verify-email', preload: VerifyEmailPage.preload },
|
|
{ path: '/forgot-password', preload: ForgotPasswordPage.preload },
|
|
{ path: '/reset-password', preload: ResetPasswordPage.preload },
|
|
{ path: '/faq', preload: FaqPublicPage.preload },
|
|
{ path: '/', preload: LandingEntry.preload },
|
|
{ path: '/share/r', preload: ShareResultVerifyPage.preload },
|
|
{ path: '/home', preload: HomePage.preload },
|
|
{ path: '/games', preload: GamePlayRoute.preload },
|
|
{ path: '/wallet', preload: WalletPage.preload },
|
|
{ path: '/referral', preload: ReferralPage.preload },
|
|
{ path: '/profile', preload: ProfilePage.preload },
|
|
{ path: '/help/faq', preload: FaqPage.preload },
|
|
{ path: '/admin/support', preload: SupportAdminPage.preload },
|
|
{ path: '/admin/faq', preload: FaqAdminPage.preload },
|
|
{ path: '/dashboard', preload: DashboardPage.preload },
|
|
{ path: '/options', preload: OptionsPage.preload },
|
|
{ path: '/admin/users', preload: UserManagementPage.preload },
|
|
{ path: '/admin/reports', preload: ReportsHubPage.preload },
|
|
{ path: '/admin/settings', preload: SettingsPage.preload },
|
|
{ path: '/admin/catalog', preload: CatalogAdminPage.preload },
|
|
{ path: '/admin/treasury', preload: TreasuryAdminPage.preload },
|
|
{ path: '/admin/approvals', preload: ApprovalsPage.preload },
|
|
{ path: '/admin/withdrawals', preload: WithdrawalsAdminPage.preload },
|
|
{ path: '/admin/approval-policies', preload: ApprovalPoliciesPage.preload },
|
|
{ path: '/admin/security', preload: SecurityAdminPage.preload },
|
|
{ path: '/admin/referral/regularization', preload: ReferralRegularizationPage.preload },
|
|
{ path: '/admin/referral/fraud', preload: ReferralFraudDashboardPage.preload },
|
|
{ path: '/admin/referral', preload: ReferralAdminPage.preload },
|
|
{ path: '/admin/test-wallet', preload: TestWalletAdminPage.preload },
|
|
{ path: '/admin/share-cards', preload: ShareCardsAdminPage.preload },
|
|
{ path: '/admin/solo-encouragement', preload: SoloEncouragementAdminPage.preload },
|
|
{ path: '/admin/marketing', preload: MarketingAdminPage.preload },
|
|
{ path: '/admin/countries', preload: CountriesAdminPage.preload },
|
|
{ path: '/admin/network', preload: NetworkStatusPage.preload },
|
|
{ path: '/admin/rbac', preload: RoleManagementPage.preload },
|
|
{ path: '/admin/admin-users', preload: AdminUsersPage.preload },
|
|
{ path: '/admin/mobile-versions', preload: MobileVersionsAdminPage.preload },
|
|
{ path: '/admin/ssl-certificates', preload: SslCertificatesAdminPage.preload },
|
|
{ path: '/admin/vps-monitor', preload: VpsMonitorAdminPage.preload },
|
|
].sort((a, b) => b.path.length - a.path.length);
|
|
|
|
/** Warm the JS chunk for a destination before navigation. */
|
|
export function prefetchRoute(to: string): void {
|
|
const path = to.split('?')[0]?.split('#')[0] || to;
|
|
const match = PATH_PRELOAD.find(
|
|
(entry) => path === entry.path || path.startsWith(`${entry.path}/`)
|
|
);
|
|
if (!match) return;
|
|
void match.preload();
|
|
}
|
|
|
|
/** Prefetch game play shell + a specific implementation module. */
|
|
export function prefetchGamePlay(implementationKey?: string | null): void {
|
|
void GamePlayRoute.preload();
|
|
if (!implementationKey) return;
|
|
void import('../../features/games/registry').then((m) => {
|
|
m.preloadGameImplementation(implementationKey);
|
|
});
|
|
}
|