Performance: Preloading strategies
Preloading strategies affect how quickly lazy-loaded modules become available, impacting page load speed and interaction readiness.
Jump into concepts and practice - no test required
RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })RouterModule.forRoot(routes, { preloadingStrategy: NoPreloading })| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| NoPreloading | Minimal until navigation | 1 reflow per lazy load | Paint delayed on navigation | [X] Bad |
| PreloadAllModules | Minimal upfront | Single reflow after preload | Faster paint on navigation | [!] OK |
| CustomPreloadingStrategy | Minimal and selective | Single reflow for critical modules | Balanced paint timing | [OK] Good |
preloadingStrategy property in router config to set preloading behavior.PreloadAllModules is the built-in strategy to preload all lazy modules after app loads.const routes = [
{ path: 'home', loadChildren: () => import('./home/home.module').then(m => m.HomeModule) },
{ path: 'admin', loadChildren: () => import('./admin/admin.module').then(m => m.AdminModule) }
];
@NgModule({
imports: [RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })],
exports: [RouterModule]
})
export class AppRoutingModule {}PreloadAllModules, which preloads all lazy modules after app startup.loadChildren.const routes = [
{ path: 'dashboard', loadChildren: './dashboard/dashboard.module#DashboardModule' }
];
@NgModule({
imports: [RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })],
exports: [RouterModule]
})
export class AppRoutingModule {}PreloadingStrategy to control which modules preload.data.preload flag in routes, the strategy can preload only selected modules like 'admin'.