Code splitting doesn't remove a network request — it reschedules it to the moment a user clicks, against a build that may no longer exist. Measured in a small React shop: why a 158 ms chunk still shows a spinner for 300 ms, how to get the click to 1 ms, and what breaks after a deploy.
Say you run a small shop. Three pages: a catalog, a cart, and a checkout.
The checkout validates the card form with zod, and zod is big. So you do
the sensible thing and split it off:
const Checkout = lazy(() => import('./pages/Checkout.jsx'));
The build agrees with you. The first file a visitor downloads drops from
299 kB to 222 kB, and the checkout code — zod included, 78 kB of it — moves
into its own chunk. Job done.
Except you didn’t remove anything. That 78 kB still has to arrive before the checkout page can render. You moved it. Here’s the reframe I want you to hold for the rest of this post:
A lazy route is a network request you’ve scheduled for later.
“Later” has two meanings, and each one causes its own problem. Later is the click — the one moment the user is actively waiting on you. And later can be after a deploy — when the file you scheduled may no longer exist.
I built the shop with React 19 and Vite 8, served it from a small local server that holds every response for 150 ms (a believable round trip), and measured both problems. Every number below is from those runs.
Part 1: later is the click
What a split point turns into
Both the cart and the checkout use a tiny shared helper, formatPrice. Here is
what the lazy build produced:
assets/price-9ISNwSuK.js 0.09 kB
assets/Cart-CcEDL5ut.js 0.24 kB
assets/Checkout-CgEbwxjk.js 78.39 kB
assets/index-CE_evALS.js 222.49 kB
Two import() calls, four files. The bundler noticed that two lazy chunks
share formatPrice, and gave it its own chunk rather than copying it into
both — or pushing it into the entry, where every visitor would pay for it.
That small chunk matters, because chunks import each other. The top of the checkout chunk looks like this:
import{t as e}from"./index-CE_evALS.js";import{t}from"./price-9ISNwSuK.js";
So loading the checkout is not one request. It’s one request, and then — once the browser has read that file and found the import — another.
The waterfall, and the fix you already have
That “and then” is a waterfall. The browser can’t know a chunk needs price
until the chunk has arrived. With a 150 ms round trip, each level costs 150 ms.
But the bundler does know, at build time. So Vite writes the dependency list
for each split point into the entry, and wraps every import() in a small
preload helper that starts the whole list at once using
<link rel="modulepreload">. You can see the list sitting in the built entry:
const __vite__mapDeps = (i, m = __vite__mapDeps, d = (m.f || (m.f = [
"assets/Cart-CcEDL5ut.js", "assets/price-9ISNwSuK.js", "assets/Checkout-CgEbwxjk.js"
]))) => i.map(i => d[i]);
// …
import(`./Checkout-CgEbwxjk.js`), __vite__mapDeps([2, 1]) // Checkout + price
To see what it’s saving you, I built the shop again with
build: { modulePreload: false }, which (I checked the output) drops that list
entirely. Then I clicked Checkout in each build:
preload on Checkout +3 → +158 ms
price +3 → +157 ms
preload off Checkout +3 → +159 ms
price +163 → +316 ms
One extra round trip, exactly as predicted. On by default, which is good.
Without the preload helper: every level of chunk-imports-chunk costs a full round trip, and real apps have several levels.
So the network side is solved. Now watch what the user actually sees.
The chunk is fast. The screen isn’t.
I timed one more thing: how long after the click the checkout text actually appeared on screen. Here are the first two builds again, with that column added:
| Chunks ready | Checkout visible | |
|---|---|---|
| Preload on | +158 ms | 307 ms |
| Preload off | +316 ms | 322 ms |
Half the network time, and the user sees almost no difference. Both show a loading fallback, and both take about 300 ms. Something other than the network is setting the pace.
That something is React, and two rules from its docs explain it.
The first: lazy doesn’t load anything until it renders. In the docs’ words,
“React will not call load until the first time you attempt to render the
returned component.” When it does, it gets back a Promise. Even if the module
arrived long ago, a Promise isn’t settled on the line you get it, so the
component suspends — and the fallback goes up.
The second is in the Suspense caveats: “React reveals suspended content at
most once every 300ms, measured from the last reveal.” Showing the fallback
was a reveal. So the real page can’t follow it for roughly 300 ms, however
quickly the chunk landed. React’s own source says it does this to “minimize jank” as new content
streams in — but it means that at a 150 ms round trip, React’s
pacing hides your whole waterfall.
Prefetching alone doesn’t fix it
The obvious next move is to start the download before the click. The pointer often reaches the button a moment before the user presses it, so:
const loadCheckout = () => import('./pages/Checkout.jsx');
const Checkout = lazy(loadCheckout);
<button onMouseEnter={loadCheckout} onClick={() => setRoute('checkout')}>Checkout</button>
It works on the network. With the hover 500 ms before the click, both chunks
arrived in about 155 ms, and the click itself made zero requests: when lazy
later calls the same loader, import() asks for a URL the browser has already
loaded, and the browser hands back the same module.
And the user still waited 301 ms and still saw the fallback. The module
was ready; lazy didn’t know that until it rendered, so it suspended once —
and the 300 ms rule did the rest.
Don’t hide the page you’re leaving
The fix is to stop showing the fallback at all. Wrap the navigation in a transition:
const [route, setRouteNow] = useState('catalog');
const setRoute = (next) => startTransition(() => setRouteNow(next));
The React docs describe the effect plainly: during a transition, “it’s better to keep showing the previous page instead of hiding any already revealed content.” The catalog stays on screen, React prepares the checkout in the background, and swaps it in when it’s ready. No fallback, so no 300 ms throttle.
| Fallback? | Chunks ready | Checkout visible | |
|---|---|---|---|
| Transition, preload on | no — catalog stays | +160 ms | 168 ms |
| Transition, preload off | no | +315 ms | 327 ms |
| Transition + hover prefetch | no | before the click | 1 ms |
Now the screen tracks the network again — you can finally see the waterfall the preload helper saves (168 vs 327). And with a transition and a prefetch together, the checkout appeared one millisecond after the click.
If your router runs navigations as transitions, you get the first part for free; check its docs. If you route by hand, you add it yourself.
The key that cancels the fix
The first time I added the transition, nothing changed: the fallback still appeared, and the checkout still took about 300 ms. The cause was a line I’d written earlier without a second thought:
<ChunkErrorBoundary key={route}>
<Suspense fallback={<p>Loading…</p>}>
I’d put key={route} on the error boundary so that an error on one page would
clear when you moved to another. But a new key makes React throw the old
component away and build a new one — including the Suspense boundary inside
it. The docs say a changed key “resets the Suspense boundary.” A transition
only avoids hiding content that’s already on screen, and a boundary that was
just reset has nothing on screen. So React showed its fallback.
The fix was to keep the boundary’s identity and clear the error another way:
componentDidUpdate(prev) {
if (prev.resetKey !== this.props.resetKey && this.state.error) this.setState({ error: null });
}
with <ChunkErrorBoundary resetKey={route}> instead of key={route}. No
fallback, and 1 ms with the prefetch. This is the version of the bug I’d expect to ship: the transition is
right, the key quietly undoes it, and the fallback looks like it’s the
network’s fault.
(A key is the right tool when you want the fallback — moving from one
user’s profile to another, say, where showing the old user’s data would be
wrong. That’s the docs’ own example.)
Part 2: later can be after a deploy
The second meaning of “later” is the one nobody tests locally.
When the shop loads, the entry file it runs has the checkout’s file name
baked into it: import('./Checkout-CgEbwxjk.js'). That name is a content hash.
So I opened the shop, then deployed a new version — one word changed in the
checkout, built into the same folder — and clicked Checkout on the page that
was still open.
before: Cart-CcEDL5ut.js Checkout-CgEbwxjk.js index-CE_evALS.js price-9ISNwSuK.js
after: Cart-RsKRhFKV.js Checkout-CENDYZsc.js index-gWdFq1XT.js price-9ISNwSuK.js
GET /assets/Checkout-CgEbwxjk.js 404
TypeError: Failed to fetch dynamically imported module: …/assets/Checkout-CgEbwxjk.js
The old page asked for a file the deploy had deleted. Note the error: it’s the
browser’s own TypeError from a native import(), and it doesn’t mention the
404. You only see that in the network log.
Two things held up. The error boundary caught it — the lazy docs say a
rejected load is thrown “for the nearest Error Boundary to handle” — so only
the page area broke, not the whole app. And Vite fired a vite:preloadError
event on window. Its docs name this exact cause (“when a new deployment
occurs, the hosting service may delete the assets from previous
deployments”) and suggest the blunt fix:
window.addEventListener('vite:preloadError', () => window.location.reload());
A reload fetches the new index.html, which names the new chunks. For that to
work, the same docs add, index.html itself must be served with
Cache-Control: no-cache — otherwise the browser may keep an old copy that
points at old files. Keeping the previous build’s assets on the server for a
while after a deploy is the other common defence.
Why “just retry” doesn’t work
My first instinct was to retry. So I tried the simplest retry: back to the catalog, then Checkout again. Same error — and no request at all.
Part of that is React: lazy caches its Promise, rejection included, so it
never calls the loader twice. But I also ran a bare import() of the old URL
from a script on the page. It failed instantly, again with no request. In Chromium, the
page remembered that the module had failed and didn’t ask the server again.
(The HTML spec’s module-loading steps read to me as if a failed fetch should be
tried again, so other browsers may behave differently — I’ve only tested
Chromium.)
It doesn’t matter much here, because the file isn’t coming back: the deploy
deleted it. A retry of the same URL is the wrong fix. A fresh index.html is
the right one.
One change, three new names
Look at the file lists again. I changed only the checkout. Three names changed — and one of them is a file I didn’t touch:
| Chunk | Changed? | Why |
|---|---|---|
| Checkout | yes | Its code changed. |
| index | yes | It contains the name Checkout-….js. |
| Cart | yes | Its code contains from"./index-CE_evALS.js", and that name changed. |
| price | no | It imports nothing. |
A content hash covers a file’s bytes, and a file’s bytes include the names of the files it imports. So a single edit ripples up through every chunk that imports — directly or not — a chunk whose name changed. Each of those is a fresh download for returning visitors.
What a chunk drags in with it
One last experiment, because it explains why chunks are less independent than they look. On the old page, I imported the new checkout chunk myself, by its new name. Vite would never do this — the old page can’t know the new name — but it shows what’s attached to a chunk:
GET /assets/Checkout-CENDYZsc.js 200 78,395 B
GET /assets/index-gWdFq1XT.js 200 222,495 B
The new checkout imports from the new entry, so the browser fetched and ran all 222 kB of it — React included. And since the entry is what starts the app, it rendered a fresh shop straight over the old one. The cart was empty again. The page now held two copies of the entry code, each with its own React.
A chunk isn’t a free-standing piece of your app. It’s a piece of one build, held to it by the file names it imports. Mix chunks from two builds and you get two of everything they share.
The whole thing on one page
| Problem | What fixes it | Measured |
|---|---|---|
| First download too big | lazy + import() at route or feature boundaries |
299 → 222 kB |
| Chunks fetched one level at a time | The preload helper (on by default) | price ready at +158 instead of +316 ms |
| Spinner for ~300 ms even with a fast chunk | startTransition on navigation |
307 → 168 ms, no fallback |
| The round trip at the click | Prefetch on hover — with a transition | 1 ms |
| Transition still shows a fallback | No key on a parent of the Suspense boundary |
fallback → none |
| Old page, deleted chunk | Error boundary + vite:preloadError → reload; no-cache on index.html |
404 → TypeError |
| Retrying the failed chunk | Don’t — load a fresh index.html |
0 requests on retry |
The takeaway
Code splitting looks like a build setting. It’s really a scheduling decision: you’re taking a network request off the page load and putting it somewhere later. Everything else follows from asking where later lands.
It lands on the click, where the user is waiting — so fetch everything in one round, start before the click, and don’t let a fallback set the pace. And it lands on whatever build the server holds at that moment — so treat a missing chunk as “this page is out of date,” not “the network blipped,” and get a fresh page rather than retrying an address that’s gone.
Ask that question at every lazy() you write, and the spinners and the
mysterious post-deploy errors stop being mysteries.