Lazy loading routes and components

Intermediate · 9 min

loadComponent and loadChildren — shipping a smaller initial bundle by only downloading a route's code when it's actually visited.

Before thisChild routes and layout composition

A Coder's Journey · 9 min

Lazy loading routes and components

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by A Coder's JourneyReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • `loadComponent` and `loadChildren` both take a dynamic `import()` instead of a direct component reference, so that route's code lands in its own separate bundle chunk instead of the main one.
  • The browser only fetches a lazy chunk the first time a user actually navigates to that route — a route nobody visits in a given session never gets downloaded at all.
  • A preloading strategy can fetch lazy chunks in the background after the initial app loads, trading a little extra network usage for near-instant navigation later — the default is no preloading at all.

Every route you configure ships code, whether visited or not

By default, every component referenced directly in a Routes array — and everything that component itself imports — ends up in the bundle the browser downloads before your app can even start rendering. A settings page, an admin panel, a rarely-visited reports screen: all of it ships to every single user on first load, even the overwhelming majority who never navigate there in a given session. For a large app, that’s a real, measurable cost to the thing every user actually experiences — time to first render — paid entirely for code most visitors never touch.

loadComponent and loadChildren: a dynamic import instead of a direct reference

const routes: Routes = [
  {
    path: 'admin',
    loadComponent: () => import('./admin.component').then((m) => m.AdminComponent),
  },
  {
    path: 'reports',
    loadChildren: () => import('./reports.routes').then((m) => m.REPORTS_ROUTES),
  },
];

The structural change is small but consequential: instead of a direct reference to a component class, loadComponent takes a function returning a dynamic import(). Bundlers (the esbuild-based builder current Angular projects use included) recognize dynamic import() calls as split points — code reached only through one gets placed in its own separate chunk, rather than folded into the main bundle. loadChildren does the same thing one level up, for an entire child-routes configuration rather than a single component, which is how whole feature areas (not just one page) get split off together.

The practical effect: the admin route’s code simply isn’t present in what the browser downloads on initial load. It’s fetched — lazily, hence the name — the first time a user actually navigates to /admin. Someone who never visits that route in a session never downloads its code at all.

Preloading: trading some bandwidth for less waiting later

Lazy loading’s one real downside is that first visit to a lazy route pays a network round-trip that an eagerly-loaded route never would have. A preloading strategy addresses that by fetching lazy chunks in the background, after the initial app has already loaded and rendered, so they’re often already cached by the time a user actually navigates there:

provideRouter(routes, withPreloading(PreloadAllModules));

PreloadAllModules is the bluntest option — fetch every lazy chunk, eventually, regardless of whether a given user is likely to need it. Angular also supports writing a custom preloading strategy for more selective fetching (preload only the routes a user is statistically likely to visit next, say). The right choice depends entirely on your app’s actual usage patterns — preloading everything defeats some of lazy loading’s bandwidth savings if most users genuinely never touch most routes, while no preloading at all means every first visit to any lazy route pays the full network cost.

Seeing the split for yourself

It’s worth actually verifying this rather than taking it on faith: run a production build (ng build) and look at the generated output directory. A route wired through loadComponent/loadChildren produces its own separate, named chunk file, distinct from the main bundle — you can open it directly and confirm the lazy route’s code genuinely isn’t present anywhere in the files a user downloads on first load. Pairing this with the browser’s network tab, watching a new chunk request fire the moment you navigate to the lazy route for the first time, is the clearest possible demonstration that this isn’t just a build-time abstraction — it’s a real, observable difference in what crosses the network and when.

This lesson’s video sets up lazy loading and preloading together, in the context of a real, ongoing standalone-components app — the linked “deeper” video covers the same mechanics from a different angle if a second worked example helps it click.

Check yourself

  1. 01What's the structural difference between `{ path: 'admin', component: AdminComponent }` and `{ path: 'admin', loadComponent: () => import('./admin.component').then(m => m.AdminComponent) }`?

    The first imports `AdminComponent` directly, so its code is bundled into whatever chunk contains the route configuration — typically the main bundle, downloaded on initial app load whether or not the user ever visits `/admin`. The second uses a dynamic `import()`, which most bundlers automatically split into its own separate chunk, fetched over the network only when a user actually navigates to `/admin`.

  2. 02With no preloading strategy configured, what happens the very first time a user navigates to a lazily-loaded route — does it render instantly?

    No — the browser has to fetch that route's chunk over the network before it can render anything, so there's a brief delay (depending on connection speed and chunk size) the first time, compared to an eagerly-loaded route whose code was already downloaded as part of the initial bundle. Subsequent visits to the same route within the session are typically fast, since the chunk is already cached.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark