Course contentsShow
← React
Lesson 1410 of 2,39635. Code SplittingFree lesson

The Prefetch/Import Timing Tradeoff

Balance between eager prefetch (faster UX, more data) and lazy import (smaller bundle, slight delay).

The Prefetch/Import Timing Tradeoff

You'll learn how to balance when you load code-split chunks to optimize both bundle size and user experience.

The Core Tension

When you lazy-load components, you face a timing decision: load the code early (prefetch/preload) or late (on-demand import). Each choice has tradeoffs:

  • Eager prefetch: You load the chunk during idle time or on hover, making the component feel instant when needed. Users get a faster experience, but you've downloaded more JavaScript upfront—sometimes code they never use.

  • Lazy import: You wait until the exact moment the component is needed (like when a modal opens). This keeps your initial bundle minimal, but users experience a brief loading delay.

Think of it like packing for a trip: bring everything you might need (heavier bag, but prepared), or pack light and buy locally if necessary (lighter load, but you'll wait at stores).

Finding the Balance

The right choice depends on:

  • Likelihood of use: High-traffic features deserve prefetch. Rare admin panels don't.
  • Chunk size: Prefetching a 5KB component is cheap. A 500KB charting library? Wait until needed.
  • User context: On hover, prefetch aggressively. On page load, be conservative.

You've already seen preloading on hover—that's the middle ground. You defer the initial load but anticipate user intent, delivering near-instant interactions without bloating the startup bundle.

Key Takeaway: The prefetch/import timing tradeoff requires weighing faster UX (prefetch earlier) against smaller bundles (import later). Choose based on usage likelihood, chunk size, and user interaction patterns—there's no one-size-fits-all answer.