Skip to content

Lesson 7 of 9 · 25m

Bundle Splitting That Matches Product Boundaries

Use route-level and feature-level code splitting, dependency analysis, and lazy loading that aligns with real user behavior rather than arbitrary chunk counts.

The first bundle audit I remember ended with a developer deleting a 12 KB dependency and declaring victory while the login page still shipped a 900 KB charting library. Bundle splitting is not about creating more files. It is about matching delivery to product boundaries: what this user needs now, what they may need later, and what they may never need.

Measure the actual graph

Do not guess from package names. Analyze production output:

bun run build
npx source-map-explorer dist/assets/*.js
# or
npx vite-bundle-visualizer

Look for large dependencies, accidental duplication, eager imports, and shared chunks quietly absorbing feature-specific libraries. A small package can pull in locale data, icons, editor cores, markdown parsers, or polyfills.

Split at route boundaries first

Route-level splitting is usually the highest-value move because users do not visit every route:

const AdminConsole = lazy(() => import('./routes/AdminConsole'))

<Route path="/admin" element={
  <Suspense fallback={<RouteSkeleton />}>
    <AdminConsole />
  </Suspense>
} />

Framework routers often create route chunks automatically, but automatic splitting does not excuse careless imports. A shared utility that imports an editor can drag the editor into the common graph.

Feature splitting is for rare, heavy interactions

Choose components that are heavy, rarely used, and visually isolated:

const ChartEditor = lazy(() => import('./ChartEditor'))

return editing ? (
  <Suspense fallback={<EditorLoading />}><ChartEditor /></Suspense>
) : <ReportSummary onEdit={() => setEditing(true)} />

Do not lazy-load tiny buttons or core navigation. Each split adds a network request, a suspense boundary, loading-state design work, and possible interaction delay.

Prefetch when intent is real

Lazy loading is not a license to make users wait:

const preloadChartEditor = () => import('./ChartEditor')
<button onMouseEnter={preloadChartEditor} onFocus={preloadChartEditor}>Customize chart</button>

Framework routers often prefetch on link visibility or hover. Verify it. Excessive prefetching wastes mobile bandwidth and can hurt the users you meant to protect.

Shared chunks are architecture

If two rare admin routes share a large library, a shared async chunk can avoid duplication. If only one feature uses it, keep it out of the common path. Review the import graph after refactors; one innocent utility can pull a dependency upward.

Compression is not a substitute

Gzip and Brotli reduce transfer bytes, but parse, compile, and execute cost remains. A 300 KB gzipped bundle can still be over a megabyte of source text on the main thread.

Field notes

  • Analyze production builds; development graphs hide what ships.
  • Lazy boundaries need designed loading and error states, not blank sections.
  • Watch CSS too: component libraries may import global styles eagerly.
  • Bundle regressions are easiest to fix before the dependency becomes product logic.

Was this lesson helpful?

Sign in to rate