Code Splitting
Code splitting breaks your application into smaller chunks that load on demand, improving initial load time and overall performance.Automatic Code Splitting
React Router automatically code-splits when you uselazy():
/dashboard.
Framework Mode
In framework mode, routes are automatically code-split by default:Manual Code Splitting
Route Components
Split components withReact.lazy() and Suspense:
Utility Functions
Split heavy utilities:Third-Party Libraries
Lazy load large dependencies:Preloading
Improve perceived performance by preloading routes:Preload Links Component
Chunk Optimization
Shared Chunks
Extract common dependencies:Dynamic Import Names
Name your chunks for better debugging:Bundle Analysis
Visualize Bundle Size
Check Chunk Sizes
Loading States
Route Level
Show loading UI during route transitions:Component Level
Handle component-level loading:Error Boundaries
Handle chunk loading failures:Performance Tips
- Split by route: Most effective code splitting strategy
- Split heavy components: Charts, editors, maps
- Don’t over-split: Too many chunks increases overhead
- Preload critical routes: On app startup or hover
- Monitor bundle size: Use visualization tools
- Cache aggressively: Set long cache headers for chunks
Framework Mode Optimizations
Route Groups
Group related routes:Shared Layouts
Layouts are shared across child routes (not duplicated):Best Practices
- Start with routes: Automatic splitting per route
- Add component splitting: For heavy UI components
- Profile before optimizing: Use React DevTools Profiler
- Test on slow networks: Ensure good UX during loading
- Monitor real-world metrics: Track bundle sizes in CI