Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply 62 React and Next.js performance optimization rules from Vercel Engineering
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
rules/bundle-conditional.md
1---2title: Conditional Module Loading3impact: HIGH4impactDescription: loads large data only when needed5tags: bundle, conditional-loading, lazy-loading6---78## Conditional Module Loading910Load large data or modules only when a feature is activated.1112**Example (lazy-load animation frames):**1314```tsx15function AnimationPlayer({ enabled, setEnabled }: { enabled: boolean; setEnabled: React.Dispatch<React.SetStateAction<boolean>> }) {16const [frames, setFrames] = useState<Frame[] | null>(null)1718useEffect(() => {19if (enabled && !frames && typeof window !== 'undefined') {20import('./animation-frames.js')21.then(mod => setFrames(mod.frames))22.catch(() => setEnabled(false))23}24}, [enabled, frames, setEnabled])2526if (!frames) return <Skeleton />27return <Canvas frames={frames} />28}29```3031The `typeof window !== 'undefined'` check prevents bundling this module for SSR, optimizing server bundle size and build speed.32