Preloading strategies help your Angular app load parts of it faster by loading some modules in the background. This makes the app feel quicker when users navigate.
Preloading strategies in Angular
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Angular
RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })Use
preloadingStrategy option inside RouterModule.forRoot.Angular provides built-in strategies like
PreloadAllModules and NoPreloading.Examples
Angular
import { PreloadAllModules } from '@angular/router'; @NgModule({ imports: [ RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules }) ], exports: [RouterModule] }) export class AppRoutingModule {}
Angular
import { NoPreloading } from '@angular/router'; @NgModule({ imports: [ RouterModule.forRoot(routes, { preloadingStrategy: NoPreloading }) ], exports: [RouterModule] }) export class AppRoutingModule {}
data: { preload: true }.Angular
import { PreloadingStrategy, Route } from '@angular/router'; import { Observable, of } from 'rxjs'; export class CustomPreloadingStrategy implements PreloadingStrategy { preload(route: Route, load: () => Observable<any>): Observable<any> { return route.data && route.data['preload'] ? load() : of(null); } } @NgModule({ imports: [ RouterModule.forRoot(routes, { preloadingStrategy: CustomPreloadingStrategy }) ], providers: [CustomPreloadingStrategy], exports: [RouterModule] }) export class AppRoutingModule {}
Sample Program
This Angular routing module uses the PreloadAllModules strategy to load all lazy modules after the app starts. This improves user experience by reducing wait time when navigating.
Angular
import { NgModule } from '@angular/core'; import { RouterModule, Routes, PreloadAllModules } from '@angular/router'; const routes: Routes = [ { path: '', loadChildren: () => import('./home/home.module').then(m => m.HomeModule) }, { path: 'about', loadChildren: () => import('./about/about.module').then(m => m.AboutModule) } ]; @NgModule({ imports: [RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })], exports: [RouterModule] }) export class AppRoutingModule {} // Explanation: // This routing module sets up two lazy-loaded routes: home and about. // Using PreloadAllModules means Angular will load both modules in the background after the app starts. // This makes navigating to 'about' faster because it is already loaded quietly. // Output: // When the app starts, the home module loads immediately. // The about module loads quietly in the background. // Navigating to '/about' is instant because the module is preloaded.
Important Notes
Preloading does not block the initial app load; it happens in the background.
Use custom preloading to control which modules load early based on your app needs.
Preloading helps especially in apps with many lazy-loaded modules to improve speed.
Summary
Preloading strategies load lazy modules in the background to speed up navigation.
Angular offers built-in strategies like PreloadAllModules and NoPreloading.
You can create custom strategies to preload only specific modules.
Practice
1. What is the main purpose of preloading strategies in Angular?
easy
Solution
Step 1: Understand lazy loading in Angular
Lazy loading delays loading modules until needed, which can slow navigation initially.Step 2: Role of preloading strategies
Preloading strategies load lazy modules in the background after app startup to speed up future navigation.Final Answer:
To load lazy modules in the background to improve navigation speed -> Option AQuick Check:
Preloading = background loading for faster navigation [OK]
Hint: Preloading means loading modules quietly before needed [OK]
Common Mistakes:
- Confusing preloading with eager loading
- Thinking preloading disables lazy loading
- Assuming preloading updates Angular versions
2. Which of the following is the correct way to enable preloading of all lazy modules in Angular routing?
easy
Solution
Step 1: Identify Angular's preloading strategy option
Angular uses thepreloadingStrategyproperty in router config to set preloading behavior.Step 2: Recognize the correct strategy for all modules
PreloadAllModulesis the built-in strategy to preload all lazy modules after app loads.Final Answer:
RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules }) -> Option AQuick Check:
PreloadAllModules = preload all lazy modules [OK]
Hint: Use preloadingStrategy: PreloadAllModules to preload all [OK]
Common Mistakes:
- Using 'preload: true' which is invalid
- Confusing NoPreloading with preloading all
- Using lazyLoad option which doesn't exist
3. Given this Angular routing setup:
What happens after the 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 {}What happens after the app loads?
medium
Solution
Step 1: Analyze the preloading strategy used
The routing usesPreloadAllModules, which preloads all lazy modules after app startup.Step 2: Identify lazy loaded modules
Both 'home' and 'admin' modules are lazy loaded vialoadChildren.Final Answer:
Both 'home' and 'admin' modules preload in the background -> Option DQuick Check:
PreloadAllModules preloads all lazy modules [OK]
Hint: PreloadAllModules preloads every lazy module automatically [OK]
Common Mistakes:
- Assuming only first lazy module preloads
- Thinking modules load only on demand with PreloadAllModules
- Confusing eager loading with preloading
4. Identify the error in this Angular routing configuration:
const routes = [
{ path: 'dashboard', loadChildren: './dashboard/dashboard.module#DashboardModule' }
];
@NgModule({
imports: [RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })],
exports: [RouterModule]
})
export class AppRoutingModule {}medium
Solution
Step 1: Check loadChildren syntax
The string syntax ('./dashboard/dashboard.module#DashboardModule') is deprecated in Angular; dynamic import is required.Step 2: Confirm preloading strategy and other options
Preloading strategy can be used with lazy loading; no error there. 'pathMatch' is optional here.Final Answer:
Using string syntax for loadChildren is deprecated; should use dynamic import -> Option BQuick Check:
Use dynamic import() for loadChildren [OK]
Hint: Use dynamic import() syntax for loadChildren [OK]
Common Mistakes:
- Using old string syntax for loadChildren
- Thinking preloading strategy conflicts with lazy loading
- Assuming pathMatch is mandatory for all routes
5. You want to preload only the 'admin' module but not the 'user' module in your Angular app. Which approach correctly implements this custom preloading strategy?
hard
Solution
Step 1: Understand custom preloading strategy
Angular allows creating a class implementingPreloadingStrategyto control which modules preload.Step 2: Use route data property to select modules
By checkingdata.preloadflag in routes, the strategy can preload only selected modules like 'admin'.Final Answer:
Create a class implementing PreloadingStrategy that preloads only routes with data.preload = true -> Option CQuick Check:
Custom strategy uses data.preload flag [OK]
Hint: Use data.preload flag with custom PreloadingStrategy [OK]
Common Mistakes:
- Using PreloadAllModules when selective preload is needed
- Trying to preload modules eagerly without strategy
- Confusing eager loading with preloading
