Component-Based Code Splitting
You'll learn how to lazy-load individual components that aren't immediately visible, reducing your initial JavaScript bundle size.
With route-based splitting (already covered), you split code by pages. Component-based splitting goes deeper—targeting individual components within a page that aren't critical for the first render.
The Core Idea
Not every component on a page needs to load instantly. Think of a product page: users see the hero image and price first, but the reviews section might be below the fold, or a chat widget might only appear when clicked. Why force the browser to download and parse JavaScript for components the user may never see?
Component-based code splitting uses dynamic imports to load these "secondary" components only when they're needed—typically when they scroll into view or when a user triggers an interaction.
When to Apply It
Target components that are:
- Below the fold (modals, tooltips, accordions)
- User-triggered (chat widgets, video players)
- Heavy dependencies (rich text editors, charting libraries)
- Conditionally rendered (admin panels visible only to certain users)
The Analogy
Imagine a restaurant menu. Route-based splitting is like having separate menus for breakfast, lunch, and dinner. Component-based splitting is like not printing the dessert section until someone asks for it—why waste paper (bundle size) if they might not order dessert?
Key Considerations
This technique pairs well with the route-based splitting you've already learned. You're essentially creating multiple split points within a single route, giving you granular control over what loads when.
Key Takeaway: Component-based code splitting lazy-loads individual components that aren't critical for initial render, reducing bundle size and improving Time to Interactive without waiting for a full route change.