What is Code Splitting?
Your dashboard has a large report builder that most users never open. The whole dashboard downloads and evaluates that code on every first visit. After splitting it out, the first page loads faster but the first click on Open report builder feels slow. How do you decide where to split and when to load the code?
Interview answer
Code splitting puts parts of the app in independently loadable chunks. Start with route boundaries and large, optional features such as the report builder. Load the code when it becomes useful, and measure both the initial route and the first use of the deferred feature. Prefetch on a clear signal of intent if the first interaction still feels slow.
Split an optional React feature
import { lazy, Suspense, useState } from 'react';const ReportBuilder = lazy(() => import('./ReportBuilder'));export default function Dashboard() {const [open, setOpen] = useState(false);return (<><button type="button" onClick={() => setOpen(true)}>Open report builder</button>{open && (<Suspense fallback={<p>Loading report builder…</p>}><ReportBuilder /></Suspense>)}</>);}
The dynamic import() gives a supporting bundler a split point. lazy() starts loading when React first renders that lazy component, and expects the module’s default export to be the component. Keep the lazy() declaration at module scope so the component identity is stable. Suspense covers the wait. An error boundary above it handles a rejected module load if the chunk cannot be fetched.
Decide when the code should arrive
| Split point | Good fit | Cost to watch |
|---|---|---|
| Route | The settings screen and dashboard serve different visits | Extra work on navigation if the route has not been loaded |
| Interaction | An editor, charting library, or export dialog is optional | The first click waits for download and evaluation |
| Intent-based prefetch | Links or buttons are often used soon after focus or pointer hover | Extra background traffic when the signal predicts the wrong action |
Inspect the production build and network waterfall to confirm that the heavy module moved out of the initial graph. A separate static import elsewhere can bring the same dependency back into that graph. Compare a cold first load, the delay from click to feature ready, and behavior on a repeat visit. Splitting tiny dependencies too aggressively can increase scheduling and request overhead.
Tree shaking removes unused exports when the build can prove they are unused. Code splitting changes when shipped code becomes available. Both can reduce the initial cost for different reasons.
Interview follow-up
After a deployment, a user with an old tab cannot open the report builder. What would you check? Look for a missing chunk URL from the previous release. Keep earlier versioned assets available during rollout where supported. Give the error boundary a recovery action, such as refreshing to load the current app shell. Re-rendering the same lazy component alone may reuse its cached rejected load.
Further reading · React lazy · Webpack code splitting
Related React Ecosystem interview topics
Continue with nearby questions from the same topic cluster.