perf: prefetch background chunk on page load from localStorage cache

- Add prefetchBackground() to registry — triggers dynamic import
  without creating React component
- Call prefetch at module init in BackgroundRenderer using cached
  config from localStorage, before React renders
- Background JS chunk starts downloading immediately instead of
  waiting for Suspense to trigger lazy() import
This commit is contained in:
Fringg
2026-02-25 08:14:13 +03:00
parent 65afb29274
commit 44d88f7653
2 changed files with 35 additions and 1 deletions

View File

@@ -3,7 +3,7 @@ import { useQuery } from '@tanstack/react-query';
import { brandingApi } from '@/api/branding';
import type { AnimationConfig, BackgroundType } from '@/components/ui/backgrounds/types';
import { DEFAULT_ANIMATION_CONFIG } from '@/components/ui/backgrounds/types';
import { backgroundComponents } from '@/components/ui/backgrounds/registry';
import { backgroundComponents, prefetchBackground } from '@/components/ui/backgrounds/registry';
const ANIMATION_CACHE_KEY = 'cabinet_animation_config';
@@ -16,6 +16,14 @@ function getCachedConfig(): AnimationConfig | null {
}
}
// Prefetch the background JS chunk immediately based on localStorage cache.
// This starts the download before React even renders, so by the time
// Suspense needs the component, the chunk is already loaded.
const cachedConfig = getCachedConfig();
if (cachedConfig?.enabled && cachedConfig.type && cachedConfig.type !== 'none') {
prefetchBackground(cachedConfig.type);
}
function setCachedConfig(config: AnimationConfig) {
try {
localStorage.setItem(ANIMATION_CACHE_KEY, JSON.stringify(config));