Code splitting and lazy loading
React.lazy and Suspense for code-splitting, why it shrinks your initial bundle, and route-based splitting as the common real-world case.
React Router fundamentalsThe React Compiler and automatic memoization
Code splitting and lazy loading
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Without code-splitting, a bundler ships every component your app could ever render in one JavaScript file, downloaded and parsed before the first screen paints.
- React.lazy turns a static import into a function that loads the component's code on demand, the first time it's actually needed, splitting it into its own chunk.
- Suspense is what shows a fallback while a lazily loaded chunk is still downloading, which is why React.lazy and Suspense are always used together.
- Splitting at each route is the most common real-world pattern, because a user on the settings page has no need for the code behind the billing page they haven't visited.
- Smaller initial bundles mean less JavaScript to download and parse before the page becomes interactive, which matters most on slow connections and lower-powered devices.
- Code-splitting is an optimization you add once an app has grown enough routes or heavy features to matter; a small app with one or two screens gets little from it.
The problem: one giant bundle
By default, every component you import ends up in the same JavaScript bundle, regardless of whether a given user will ever render it. An admin dashboard with a settings page, a billing page, and a reports page ships the code for all three to every visitor, even one who only ever opens the settings page. On a fast connection with a small app, that’s unnoticeable. On a slow connection with a large app, it means a visitor downloads and parses megabytes of code for screens they may never open before they can interact with the one screen they came for.
Code-splitting breaks that single bundle into smaller chunks that load only when needed.
React.lazy: an import that waits
React.lazy wraps a dynamic import() so the component’s code is fetched only the first time it’s rendered, instead of being part of the initial bundle:
import { lazy } from "react";
// Static import — ships in the main bundle no matter what:
// import ReportsPage from "./ReportsPage";
// Lazy import — its own chunk, fetched on first render:
const ReportsPage = lazy(() => import("./ReportsPage"));
The bundler (Vite, webpack, etc.) recognizes the dynamic import() syntax and automatically splits ReportsPage and everything it imports into a separate file, requested over the network only when ReportsPage is actually rendered for the first time.
Suspense: what shows while it loads
A lazy component’s code might take a moment to arrive over the network, and React needs to render something during that gap. Suspense provides that fallback:
import { lazy, Suspense } from "react";
const ReportsPage = lazy(() => import("./ReportsPage"));
function App() {
return (
<Suspense fallback={<div>Loading…</div>}>
<ReportsPage />
</Suspense>
);
}
React.lazy and Suspense are always paired: lazy is what makes the import asynchronous, and Suspense is what lets React pause and show a fallback while that asynchronous work is in flight, rather than rendering a half-ready component or crashing.
Route-based splitting: the common case
In practice, the most frequent place to split is at each route, because navigating to a new route is already the moment a user is waiting for new content to appear — showing a brief loading state there is expected and unobtrusive, unlike inside the middle of an already-visible page.
import { lazy, Suspense } from "react";
import { createBrowserRouter, RouterProvider } from "react-router-dom";
const Dashboard = lazy(() => import("./routes/Dashboard"));
const Settings = lazy(() => import("./routes/Settings"));
const Billing = lazy(() => import("./routes/Billing"));
const router = createBrowserRouter([
{ path: "/", element: <Dashboard /> },
{ path: "/settings", element: <Settings /> },
{ path: "/billing", element: <Billing /> },
]);
function App() {
return (
<Suspense fallback={<div>Loading…</div>}>
<RouterProvider router={router} />
</Suspense>
);
}
Now the initial bundle contains only the app shell and the router itself; Dashboard, Settings, and Billing each live in their own chunk, fetched the first time a user navigates to that route. A user who only ever visits /settings never downloads the code behind /billing at all. This is why route-based splitting is the default recommendation: routes are a natural, already-existing boundary that lines up with how users actually move through an app, so you get a meaningful bundle-size win without inventing a new splitting scheme from scratch.
Smaller initial bundle, same total code
It’s worth being precise about what code-splitting does and doesn’t change. The total amount of JavaScript in the app is the same either way. What changes is when each piece is downloaded: all at once up front, versus spread across the visit as each route or feature is actually used. That shift matters most on the first load, before the browser has anything cached, and especially on slow networks or weaker devices where parsing a large bundle before the page becomes interactive is itself a bottleneck.
What the video covers
Web Dev Simplified’s video walks through React.lazy and Suspense as a general pattern — splitting out a heavy component and watching the network tab show the separate chunk load on demand. It’s a clear demonstration of the mechanism but stays mostly at the component level rather than the route level; the Codevolution link below applies the same pattern specifically per-route with React Router, which is the framing this lesson leads with as the common real-world case.
Where this fits
Code-splitting leans on routing (to know where the natural boundaries are) and on understanding re-renders and the compiler (to know this is a loading-time optimization, not a re-render optimization). Next: accessibility in React apps.
Check yourself
What does a bundler ship to the browser for a component that is statically imported at the top of a file, versus one loaded with React.lazy?
A statically imported component's code is bundled into the main chunk and downloaded on every page load regardless of whether that component ever renders, while a React.lazy component's code lives in its own separate chunk that is only requested over the network the first time that component is actually rendered.
Why must a component loaded with React.lazy be rendered inside a Suspense boundary?
Loading the component's chunk is asynchronous, and Suspense is the mechanism React uses to pause rendering that subtree and show a fallback until the lazy import's promise resolves; without a Suspense boundary above it, a lazy component has no way to communicate that it isn't ready yet.
Why is splitting at route boundaries the most common strategy rather than splitting every individual component?
A route is a natural, infrequent boundary a user actually crosses by navigating, so the code behind a route only needs to load when that route is visited; splitting every small component instead would create many tiny chunks and more network requests without a proportional benefit, since most of those components render together anyway.
Does code-splitting reduce the total amount of JavaScript an app ships, or just when it's downloaded?
Just when it's downloaded. The total code is unchanged, but it arrives in separate chunks requested on demand instead of all at once, so the initial page load downloads and parses less of it.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.