Route-Based Code Splitting
0 566
๐ฆ Route-Based Code Splitting in React
As your React application grows, so does its JavaScript bundle. Loading the entire app upfront isnโt ideal, especially for users who never visit all routes. Thatโs where route-based code splitting comes in โ it lets you load only the code needed for the current route. This is easily achievable with React.lazy and Suspense. โก๐ฆ
๐ง What Is Route-Based Code Splitting?
Route-based code splitting is the practice of loading route components only when the user navigates to them. Instead of bundling all routes into a single file, each route becomes a separate chunk that loads on demand.
This improves:
- โก Initial load performance
- ๐ Time to first paint
- ๐ฒ Mobile performance and bandwidth usage
โ๏ธ How to Implement It
Hereโs a basic example using React Router v6+ with React.lazy and Suspense.
import { BrowserRouter, Routes, Route } from 'react-router-dom';
import React, { Suspense, lazy } from 'react';
const Home = lazy(() => import('./pages/Home'));
const About = lazy(() => import('./pages/About'));
const Contact = lazy(() => import('./pages/Contact'));
function App() {
return (
<BrowserRouter>
<Suspense fallback={<div>Loading...</div>}>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
<Route path="/contact" element={<Contact />} />
</Routes>
</Suspense>
</BrowserRouter>
);
}
๐ฆ Behind the Scenes
- ๐ Each route is its own file (or "chunk") thanks to Webpack/Vite bundling
- ๐ The chunk is fetched only when the route is visited
- โณ While the chunk loads, the
fallbackUI fromSuspenseis shown
๐ฌ Real-World Folder Structure
src/
โโโ App.jsx
โโโ pages/
โ โโโ Home.jsx
โ โโโ About.jsx
โ โโโ Contact.jsx
Each file in /pages is a separate route and will be split into a different bundle during build time.
๐งฉ Nested Routes & Lazy Loading
You can also apply route-based splitting to nested routes:
const Dashboard = lazy(() => import('./pages/Dashboard'));
const Settings = lazy(() => import('./pages/Settings'));
<Route path="/dashboard" element={<Dashboard />}>
<Route path="settings" element={<Settings />} />
</Route>
โ ๏ธ Things to Watch Out For
- โ ๏ธ You can only use
React.lazywith default exports - โ ๏ธ Always wrap lazy components in
Suspenseto prevent crashes - โ ๏ธ Avoid over-splitting โ loading too many chunks can hurt UX
๐ Optimizing the Fallback UI
The fallback UI shown during loading should match the user's expectations:
<Suspense fallback={<Loader type="page" />}>
<Routes> ... </Routes>
</Suspense>
You can even use different Suspense boundaries per route section for more control:
<Suspense fallback={<SidebarSkeleton />}>
<Sidebar />
</Suspense>
<Suspense fallback={<MainLoader />}>
<Routes />
</Suspense>
๐ Summary
- ๐ง Route-based code splitting loads components only when their route is visited
- โ๏ธ Implement with
React.lazyandSuspense - ๐ Reduces initial bundle size and improves performance
- โ Works well with React Router v6+
๐ Final Thoughts
Route-based code splitting is one of the easiest and most impactful optimizations you can make in a React app. It keeps your users happy with faster loads and helps your app scale effortlessly. Combine it with lazy-loaded charts, modals, and feature-heavy components to create a high-performance React experience. โ๏ธโก
If youโre passionate about building a successful blogging website, check out this helpful guide at Coding Tag โ How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!
Share:



Comments
Waiting for your comments