Code Splitting is breaking the bundle (assembled JS file) into several parts that are loaded as needed.
Simple analogy: Imagine you are watching a TV series.
How it works in React:
1. Route-based splitting (by pages):
1const Home = lazy(() => import('./pages/Home'));2const About = lazy(() => import('./pages/About'));3const Admin = lazy(() => import('./pages/Admin'));
Each page is a separate file. The user gets only the one they opened.
2. Component-based splitting (by components):
1const HeavyChart = lazy(() => import('./HeavyChart'));
The heavy chart will load only when the user clicks "Show".
3. import() — dynamic import:
1button.onclick = async () => {2 const module = await import('./utils/export');3 module.exportToExcel(data);4}
Advantages:
In Next.js Code Splitting works automatically — each page is already a separate bundle.