What is Code Splitting?
You'll learn: How code splitting breaks your JavaScript bundle into smaller chunks that load only when needed, improving initial page load performance.
The Problem with One Big Bundle
When bundlers like Webpack or Vite create your production build, they typically combine all your JavaScript into one large file. That means users download your entire application code upfront—even code for pages they might never visit. It's like downloading an entire encyclopedia when you only need one article.
How Code Splitting Works
Code splitting is a bundler feature that breaks your application into multiple smaller bundles (called "chunks"). Instead of shipping one massive 500KB file, you might have:
- A 50KB main bundle with critical code
- A 100KB chunk for your admin dashboard (loaded only when accessed)
- A 30KB chunk for your image gallery component
- Separate chunks for third-party libraries
The bundler intelligently determines what code belongs in which chunk, and the browser loads these chunks on demand—when the user actually needs them.
The On-Demand Loading Benefit
When someone visits your homepage, they only download the homepage chunk. If they navigate to the settings page, then the settings chunk downloads. Users pay the performance cost only for features they actually use.
This dramatically reduces your initial bundle size, which means faster first page loads, better Core Web Vitals scores, and happier users on slow connections or mobile devices.
Think of it like streaming a TV show episode-by-episode rather than downloading the entire season before you can watch anything.
Key Takeaway: Code splitting divides your application into smaller JavaScript chunks that load on demand, reducing initial bundle size and improving performance by only loading code when users actually need it.