What is Preloading in Angular Routing?
π‘ Concept Name
Preloading in Angular Routing β A strategy that allows Angular to load lazy-loaded modules in the background after the initial application load, improving perceived performance for users.
π Quick Intro
Angularβs preloading strategies enable lazy-loaded modules to be fetched in the background once the app is bootstrapped. This bridges the gap between lazy loading and user experience by preparing modules before theyβre needed.
π§ Analogy / Short Story
Imagine you're visiting a theme park. While you enjoy the first ride (main module), staff members quietly prepare the next rides (lazy modules) in the background so you donβt have to wait when you reach them. Thatβs Angular preloading.
π§ Technical Explanation
- πΊοΈ Angular supports
PreloadAllModules
and custom preload strategies. - β±οΈ Preloading happens after the app finishes loading the initial bundle.
- βοΈ Configured via the
RouterModule.forRoot()
method withpreloadingStrategy
. - π You can create a custom strategy to preload only specific modules based on conditions.
π» Real Code Example
// app-routing.module.ts
import { NgModule } from '@angular/core';
import { RouterModule, Routes, PreloadAllModules } from '@angular/router';
const routes: Routes = [
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: 'home', 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 { }

β Interview Q&A
Q1: What is preloading in Angular?
A: Preloading allows Angular to load lazy modules in the background after the initial load.
Q2: Whatβs the default behavior if no preloading strategy is set?
A: Lazy modules load only when their route is visited.
Q3: How do you enable preloading for all modules?
A: Use PreloadAllModules
strategy in RouterModule.forRoot()
.
Q4: Can we implement custom preloading logic?
A: Yes, by creating a custom preloading strategy class.
Q5: Does preloading affect the initial load time?
A: No, it happens in the background after initial load to avoid delays.
π MCQs
Q1. What is the purpose of preloading in Angular?
- Lazy load on click
- Load all upfront
- Load modules in background
- Disable lazy loading
Q2. Which Angular option enables all modules to preload?
- LoadAllModules
- PreloadAllModules
- AutoPreload
- EagerLoading
Q3. When does preloading occur in Angular?
- During build
- Before app loads
- After app loads
- While rendering routes
Q4. Can you preload specific routes?
- No
- Only with eager loading
- Yes, using custom strategy
- Only with guards
Q5. Where is preloading strategy configured?
- AppComponent
- NgModule
- RouterModule.forRoot()
- Angular.json
π‘ Bonus Insight
You can use route data to guide custom preloading strategies, enabling smarter decisions like only preloading based on user roles or device types.
π PDF Download
Need a handy summary for your notes? Download this topic as a PDF!