Sub topic 5
Lazy-loading features with the router
This chapter assumes you know TypeScript, standalone components, dependency injection, and the basics of the Angular Router. Here you will make each feature load only when a user first navigates to it.
Why lazy-load by feature
If every feature is imported from the app's root route file, the bundler puts them all into the first download. A user who only opens the customer list still pays for the order screens. Lazy loading moves each feature into its own chunk. The browser fetches that chunk the first time the user navigates to the feature's URL prefix.
The router does this with dynamic import(). The bundler sees import('./orders/orders.routes') and splits that file, and everything only it uses, into a separate chunk.
The app-level routes file
The root route file should only map URL prefixes to features. It should hold no feature logic.
import { Routes } from '@angular/router';
export const routes: Routes = [
{
path: 'orders',
loadChildren: () =>
import('./orders/orders.routes').then((m) => m.ORDERS_ROUTES),
},
{
path: 'customers',
loadChildren: () =>
import('./customers/customers.routes').then((m) => m.CUSTOMERS_ROUTES),
},
];loadChildren takes a function that returns a promise of a Routes array. The .then step picks the exported routes constant out of the loaded module.
The feature's own routes
Each feature owns a route file. Paths here are relative to the prefix set by the parent, so '' means /orders and ':id' means /orders/:id.
import { Routes } from '@angular/router';
export const ORDERS_ROUTES: Routes = [
{
path: '',
children: [
{
path: '',
loadComponent: () =>
import('./feature/order-list.component').then((m) => m.OrderListComponent),
},
{
path: ':id',
loadComponent: () =>
import('./feature/order-detail.component').then((m) => m.OrderDetailComponent),
},
],
},
];loadChildren loads a set of routes. loadComponent loads a single standalone component. Use loadChildren at the feature boundary and loadComponent for individual screens. Each screen then gets its own chunk, so opening the list does not download the detail page.
Common errors
- Importing the feature statically as well. If any eagerly loaded file has
import { OrderListComponent } from './orders/...', the bundler keeps that code in the main chunk. The lazy route still works, but nothing is saved. Search for static imports of feature files outside the feature. - Forgetting the named export.
.then((m) => m.ORDERS_ROUTES)returnsundefinedif the name is wrong, and navigation fails at runtime. Keep the constant name consistent with the file name. - Using a default export without handling it. If a file uses
export default, you can writeloadChildren: () => import('./orders/orders.routes')without.then. Mixing the two styles in one app is confusing, so choose one. - Putting the feature's full path in its own routes. If
ORDERS_ROUTESstarts withpath: 'orders', the URL becomes/orders/orders. The prefix belongs only to the parent.
Preloading
A lazy chunk is fetched when the user clicks, so there is a short delay on the first visit. You can reduce it by preloading chunks after the first render. Pass a strategy when you configure the router.
import { provideRouter, withPreloading, PreloadAllModules } from '@angular/router';
export const appConfig = {
providers: [provideRouter(routes, withPreloading(PreloadAllModules))],
};PreloadAllModules downloads every lazy chunk in the background once the app is idle. This keeps the small first load and removes most waits. For very large apps with rarely used features, a custom strategy that preloads only selected routes costs less bandwidth.
Summary of the pattern
| Mechanism | Loads | Typical place |
|---|---|---|
| `loadChildren` | A feature's route array | App-level routes file |
| `loadComponent` | One standalone component | Feature route file |
| `withPreloading` | Lazy chunks in the background | Router configuration |
Exercise
A developer adds lazy loadChildren for /customers, but ng build shows no separate customers chunk. What is the most likely cause, and how do you find it?
Show answer
Some eagerly loaded file, such as app.component.ts or a shared file, imports something from the customers feature with a static import. The bundler then keeps that code in the main chunk. Search the code outside the customers folder for imports from ./customers/ and remove them, or reach the feature only through the router.
Exercise
The route file for the orders feature contains path: 'orders' at its top level, and the app routes file also uses path: 'orders' for it. What URL does the list screen get, and how do you fix it?
Show answer
The list gets /orders/orders. Change the feature's top-level route to path: '', so only the parent supplies the orders prefix.