Sub topic 6
Splitting by route
This chapter assumes you know function components, useState, ES modules, and how React.lazy and Suspense work together. It also assumes you use a bundler such as Vite or webpack, and a router such as React Router. You will split an app so each page loads only when a user visits it.
Why routes are a good split point
A route is a natural boundary. A user sees one page at a time, so the code for the other pages is wasted bytes on first load. Splitting by route gives you:
- Clear chunk boundaries that match how people use the app.
- A loading moment that users already expect, because navigation is a visible event.
- Little code to change. You wrap the page components, not the code inside them.
Splitting individual widgets such as a chart or a modal also helps, but route splitting usually gives the biggest cut to the initial bundle for the least effort.
Lazy-loading each page
Replace static page imports with lazy calls. Each import() becomes its own chunk.
import { lazy, Suspense } from 'react';
import { BrowserRouter, Routes, Route } from 'react-router-dom';
const Home = lazy(() => import('./pages/Home'));
const Settings = lazy(() => import('./pages/Settings'));
export default function App() {
return (
<BrowserRouter>
<Suspense fallback={<p>Loading page...</p>}>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/settings" element={<Settings />} />
</Routes>
</Suspense>
</BrowserRouter>
);
}Both Home and Settings must have a default export. If a page uses a named export, map it to default inside the import promise, as shown in the earlier chapter on React.lazy.
One Suspense boundary sits above Routes. When any lazy page is still downloading, React shows the fallback in place of the whole routed area. Once the chunk arrives, React renders the page.
What happens during navigation
The sequence below shows a first visit to /settings.
On later visits, the module is already loaded. The browser and bundler cache it, so the page renders without a fallback.
Choosing boundary placement for routes
The single boundary above Routes is simple, but it replaces the entire page area, including shared layout parts that live inside it. If your app has a header and sidebar, keep them outside the boundary so they stay visible while the page loads.
import { lazy, Suspense } from 'react';
import { BrowserRouter, Routes, Route, Link } from 'react-router-dom';
const Home = lazy(() => import('./pages/Home'));
const Settings = lazy(() => import('./pages/Settings'));
function Layout({ children }) {
return (
<div>
<nav>
<Link to="/">Home</Link> <Link to="/settings">Settings</Link>
</nav>
<main>{children}</main>
</div>
);
}
export default function App() {
return (
<BrowserRouter>
<Layout>
<Suspense fallback={<p>Loading page...</p>}>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/settings" element={<Settings />} />
</Routes>
</Suspense>
</Layout>
</BrowserRouter>
);
}Now the navigation stays on screen and only the main content swaps to the fallback. Use this pattern as your default.
Common mistakes
| Mistake | Effect | Fix |
|---|---|---|
| Calling `lazy` inside a component body | A new lazy component is created on every render, so state resets and the chunk reloads | Declare `lazy` calls at module level |
| No `Suspense` above a lazy route | React throws an error because nothing can show a fallback | Add a boundary above `Routes` |
| Also importing the page statically elsewhere | The bundler keeps the code in the main bundle, so splitting has no effect | Import the page only through `lazy` |
| Lazy-loading a tiny page | An extra network request costs more than the bytes saved | Keep small, always-needed pages in the main bundle |
Failures and flicker
Two problems remain with this setup. A network failure while fetching a route chunk rejects the import, and without an error boundary the whole app crashes. Also, switching between pages can replace visible content with a fallback. Later chapters cover error boundaries and transitions, which address these. For now, remember that route splitting needs both a Suspense boundary and a plan for failure.
Exercise
An app imports Settings with lazy(() => import('./Settings')) in App.jsx, but a build report still shows the Settings code in the main bundle. Name one likely cause and how to confirm it.
Show answer
Another file probably imports Settings statically, for example import Settings from './Settings' in a menu or index file. The bundler then keeps the module in the main bundle. Search the project for static imports of that module, and remove them or route them through the lazy import.
Exercise
Why should a navigation bar usually sit outside the Suspense boundary that wraps Routes?
Show answer
If the navigation bar were inside the boundary, it would be replaced by the fallback whenever a route chunk loads. Keeping it outside leaves it visible, so the user keeps their orientation and can still click other links.