Course contentsShow
← Web Development
Lesson 1945 of 2,98236. Performance Optimization TechniquesFree lesson

Prefetching and Preloading Split Chunks

Using webpackPrefetch and webpackPreload to hint which chunks should load during browser idle time.

Prefetching and Preloading Split Chunks

You'll learn how to use Webpack magic comments to hint the browser when to load code-split chunks for optimal performance.

The Problem with Plain Code Splitting

When you dynamically import chunks, they only load when requested—which can cause delays when users navigate. If you know a chunk will probably be needed soon, you can give the browser hints to load it during idle time, making future interactions feel instant.

Understanding webpackPrefetch and webpackPreload

Webpack provides two magic comments to optimize chunk loading:

// Prefetch: Load during browser idle time (low priority)
import(/* webpackPrefetch: true */ './HeavyComponent');

// Preload: Load in parallel with parent (high priority)
import(/* webpackPreload: true */ './CriticalModal');

webpackPrefetch: The Patient Loader

Prefetch tells the browser: "When you have free time, grab this chunk—I'll probably need it later." The browser downloads it during idle moments and caches it. Perfect for routes users might navigate to next.

// User is on homepage; prefetch dashboard chunk
const Dashboard = lazy(() => 
  import(/* webpackPrefetch: true */ './Dashboard')
);

Think of it like: Preheating your oven while you're still preparing ingredients—you know you'll need it, but not this instant.

webpackPreload: The Urgent Loader

Preload says: "I need this chunk very soon, load it now alongside the parent." It downloads immediately with medium-high priority. Use for chunks required within seconds of the current interaction.

// Loading a critical feature needed immediately after login
import(/* webpackPreload: true */ './UserProfile');

The difference: Prefetch waits for idle time; preload starts immediately but doesn't block.

When to Use Each

  • Prefetch: Next likely routes, features behind tabs, upcoming modal content
  • Preload: Immediate child dependencies, critical above-the-fold components

Key Takeaway: Use webpackPrefetch to load chunks during idle time for likely-needed features, and webpackPreload for chunks needed almost immediately—both improve perceived performance without blocking initial load.