Part of my job is to keep React apps fast as they grow, and code-splitting is the tool I use most. It is also easy to get wrong. You cut the first download in half, and the app feels slower, because now every click waits for a download.
Code-splitting means the bundler cuts your JavaScript into chunks, and the browser downloads each chunk only when the app needs it. React gives you two pieces for this: lazy() and <Suspense>.
Start with the routes
Most of the code that a user downloads on the first visit belongs to pages that they have not opened. Routes are the natural place to cut.
import { lazy, Suspense } from "react";
import { Routes, Route } from "react-router-dom";
import Home from "./pages/Home"; // the first page stays in the main bundle
const Reports = lazy(() => import("./pages/Reports"));
const Settings = lazy(() => import("./pages/Settings"));
export default function App() {
return (
<Suspense fallback={<PageSkeleton />}>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/reports" element={<Reports />} />
<Route path="/settings" element={<Settings />} />
</Routes>
</Suspense>
);
}
Keep the page that most users see first in the main bundle. If you lazy-load it, the browser needs one more round trip before anything shows.
Then the heavy components
After the routes, look for large components that are not on screen at the start: charts, rich-text editors, date pickers, modals. A chart library can weigh more than the rest of the page.
const SalesChart = lazy(() => import("./SalesChart"));
function Dashboard() {
const [showChart, setShowChart] = useState(false);
return (
<>
<button onClick={() => setShowChart(true)}>Show chart</button>
{showChart && (
<Suspense fallback={<ChartSkeleton />}>
<SalesChart />
</Suspense>
)}
</>
);
}
Start the download before the click
A lazy chunk downloads when React first renders it. You can start earlier. The bundler fetches each chunk once, so an early import() call only warms the cache:
const loadReports = () => import("./pages/Reports");
const Reports = lazy(loadReports);
<Link to="/reports" onMouseEnter={loadReports} onFocus={loadReports}>
Reports
</Link>
Hover and focus give you a few hundred milliseconds. That is often enough for a small chunk. Phones have no hover, so for the page that users open most often next, I preload when the browser is idle:
const whenIdle = window.requestIdleCallback || ((cb) => setTimeout(cb, 1));
whenIdle(() => loadReports());
Make the wait look like the page
A spinner on a blank screen feels slower than a skeleton in the shape of the page. Keep the header and the navigation outside the <Suspense> boundary, so they never disappear. Put the boundary around the content area only.
With React 18 and later, a navigation inside a transition keeps the old screen on display while the new chunk loads, instead of the fallback. React Router 7 does this for you.
The error that most guides skip
A deploy changes the chunk file names. A user who opened the app before the deploy still runs the old main bundle, and it asks for an old chunk that is gone. The import fails. Without an error boundary, the whole app goes blank. Webpack calls this a ChunkLoadError. Vite reports “Failed to fetch dynamically imported module”.
Put an error boundary around the lazy routes and offer a reload:
class ChunkErrorBoundary extends React.Component {
state = { failed: false };
static getDerivedStateFromError() {
return { failed: true };
}
render() {
if (this.state.failed) {
return (
<p>
A new version is out.{" "}
<button onClick={() => location.reload()}>Reload</button>
</p>
);
}
return this.props.children;
}
}
This boundary catches every render error below it, not only failed downloads. At the page level, a reload is a fair answer to both.
Measure, do not guess
- Look at what is in the bundle:
rollup-plugin-visualizerfor Vite,webpack-bundle-analyzerfor webpack. - Test with a slow network and a slow CPU in DevTools. A fast laptop hides the problem.
- Compare Lighthouse before and after, and watch real-user numbers such as LCP and INP after the release.
What I ask before I split something
- Is this code on the first screen? Then it stays in the main bundle.
- Is it big? Splitting out a 3 KB component adds a request and saves nothing.
- Can the download start before the user needs it?
- What does the user see while it loads?
- What happens if the download fails?