awesome-everything RU
↑ Back to the climb

Performance

Code splitting: route-level, component-level, vendor splitting

Crux Code splitting is the highest-leverage bundle reduction: each route downloads only what it needs, components load when triggered, vendor chunks cache separately across deploys.
Your altitude — climbing toward senior
ZeroJuniorMiddleSenior
You are at middle altitude — in the sky
◷ 16 min

A dashboard app ships 900 KB of JS to every page because all routes share one bundle. The homepage needs React, a chart library, a date picker, and the admin panel — even though the homepage never shows a chart, uses the admin panel, or renders the date picker. Three routes of unneeded code travel to the user on every visit to every page.

Route-level splitting

The most impactful change. Each route gets its own JavaScript chunk; navigating to /settings does not download /editor’s bundle. Every modern bundler supports this out of the box.

  • WebpacksplitChunks configuration, or React Router 6 with lazy().
  • Vite — automatic per-route splitting when using a router plugin.
  • Next.js — automatic per-page chunks in both Pages and App Router. Every file in /pages or /(routes) is a separate chunk automatically.
  • Remix — route modules are automatically code-split; parallel data loading is built-in.

The senior pattern: every route has its own chunk. If the bundle analyzer shows that 40% of the homepage bundle is from /admin routes, that is route splitting not working.

Component-level splitting

Components that are not on the critical path — modals, sidebars, drawers, admin tools, complex charts — should load only when they are needed. The primitive is dynamic import().

// React
const DatePicker = React.lazy(() => import('./DatePicker'));
// Render only inside a <Suspense> boundary

// Vue
const DatePicker = defineAsyncComponent(() => import('./DatePicker.vue'));

The rule of thumb: any component over 30 KB that is not rendered on first paint should be lazy-loaded. Common candidates: rich text editors, data visualisation libraries, map components, video players, admin panels.

A lazy-loaded component defers its download until the first render. When the user triggers it (opens a modal, navigates to a sub-route), the chunk downloads and parses on the spot. To avoid the visible delay on that first interaction, prefetch the chunk when the user hovers or when the trigger is nearby:

<link rel="prefetch" href="/chunk-datepicker.js" />

Or use React.lazy with import(/* webpackPrefetch: true */ './DatePicker').

Vendor splitting

Third-party libraries (React, React DOM, charting libraries, utility packages) change infrequently compared to your own application code. If they are bundled with your app code, every new deploy invalidates the user’s cached copy of both — the user re-downloads React even though React did not change.

Vendor splitting puts third-party libraries in a separate chunk with its own content hash. The user caches vendor.abc123.js indefinitely. A deploy that only changes your app code invalidates app.def456.js but the vendor chunk stays cached. Second visits after a deploy download only the small changed chunk.

How to configure: Webpack’s splitChunks.cacheGroups, Vite’s manualChunks, Next.js’s automatic framework chunk (framework-*.js in _next/static/chunks/).

StrategyMechanismBest for
Route-levelEach route in its own chunk via bundler or routerSaaS apps with distinct routes sharing little code
Component-leveldynamic import() + React.lazy / defineAsyncComponentModals, drawers, charts, editors not on critical path
Vendor splittingThird-party libs in separate content-hashed chunkApps that deploy frequently; libraries change rarely

The critical path boundary

Not all lazy loading is free. Deferring a component means the first time the user opens it, the chunk downloads and parses in the foreground. This improves LCP (smaller initial bundle) but can hurt INP for that specific interaction.

The rule: keep on the critical path anything the user sees within the first 2-3 seconds of a page load. Defer everything else. If a component is below the fold, behind a click, or on a route the user visits second, it belongs in a lazy chunk.

Why this works

Why does vendor splitting help even if the vendor chunk is large? Cache headers. Static assets are served with Cache-Control: public, max-age=31536000, immutable because the filename includes a content hash. Once the user has downloaded vendor.abc123.js, it never downloads again unless the hash changes. Only your app code changes on every deploy.

Order the steps

Order the steps of bringing an existing 1.2 MB bundle under a 200 KB per-route budget:

  1. 1 Open bundle analyzer and sort modules by size
  2. 2 Identify which modules are not used on the current route
  3. 3 Set up route-level splitting via the framework router
  4. 4 Lazy-load large components not on the critical first-paint path
  5. 5 Move third-party libraries to a vendor chunk with a content hash
  6. 6 Re-run bundle analyzer and verify each route is under budget
  7. 7 Add a CI size-limit gate to enforce the budget on future PRs
Quiz

After lazy-loading a 100 KB chart component, LCP improves but a user reports a visible delay when they first click 'Open Chart'. What is the cause and the fix?

Quiz

A daily-deploying Next.js app has one large bundle with React, app code, and libraries mixed. After vendor splitting, users on a second visit after a deploy report the page loads faster. Why?

Quiz

A product engineer wants to add a rich text editor (180 KB) to the homepage. What is the senior recommendation?

Recall before you leave
  1. 01
    Explain route-level, component-level, and vendor splitting. When is each the right choice?
  2. 02
    A lazy-loaded component causes visible delay on first interaction. What is the fix?
Recap

Code splitting is the most impactful bundle optimisation: route-level ensures each page pays only for its own code, component-level defers large non-critical components, vendor splitting preserves cached library chunks across frequent deploys. The primitive for the latter two is dynamic import(), which every modern bundler and framework wraps into React.lazy, defineAsyncComponent, or equivalent. All three strategies stack. The next lesson covers tree shaking and compression — complementary techniques that reduce bytes within each chunk.

Connected lessons
appears again in159
Continue the climb ↑Tree shaking and compression: removing what you don''''t use
shortcuts expand
search
K
prev piece
k
next piece
j
cycle tier
t
this menu
?
sources3
expand
  1. 01
  2. 02
  3. 03

Trademarks belong to their respective owners. Editorial reference only.