Code splitting divides an application into separately loadable bundles, allowing feature code to load when needed instead of inflating the initial download. In React, dynamic imports combine with lazy loading and Suspense to support this approach. section 5.1 section 3.4
Use code splitting to support large, independently owned application areas:
- Segment routes by business domain, such as checkout, profile, and products, so feature ownership maps clearly to application structure. section 5.1
- Load route components on demand with React.lazy and display a Suspense fallback while the feature bundle loads. section 5.1
- Explain the trade-off in interviews: smaller initial bundles improve performance, while route loading and shared contracts require deliberate design. section 5.1
Modular route loading
A React application can lazy-load each domain and place the routes beneath one loading boundary: const CheckoutApp = lazy(() => import('../features/checkout/CheckoutApp')); <Suspense fallback={<div>Loading...</div>}> ... <Route path="/checkout/*" element={<CheckoutApp />} /> ... </Suspense>. This keeps checkout code out of the initial load until its route is needed. section 5.1
Listing 5.1 Modular route application
const CheckoutApp = lazy(() =>
import('../features/checkout/CheckoutApp')); #A
const UserProfileApp = lazy(() =>
import('../features/profile/UserProfileApp'));
const ProductInventoryApp = lazy(
() => import('../features/products/ProductInventoryApp')
);
const ModularRouteApp = () => {
return (
<BrowserRouter>
<Suspense fallback={<div>Loading...</div>}> #B
<nav #C
style={{
display: 'flex',
gap: '10px',
marginBottom: '20px',
}}
>
<Link to="/checkout">Checkout</Link>
<Link to="/profile">Profile</Link>
<Link to="/products">Products</Link>
</nav>
<Routes> #D
<Route path="/" #E
element={<Navigate to="/products" replace />} />
<Route path="/checkout/*" element={<CheckoutApp />} />
<Route path="/profile/*" element={<UserProfileApp />} />
<Route path="/products/*" element={<ProductInventoryApp />} />
<Route path="*" element={<div>404: Page Not Found</div>} /> #F
</Routes>
</Suspense>
</BrowserRouter>
);
};
Inspect the complete modular route application, including navigation, route definitions, lazy-loaded domain applications, and the loading boundary. section 5.1



