Lazy Loading
Load components and modules only when the user navigates to them โ faster initial load, smaller bundles.
Lazy loading means loading code ONLY when the user navigates to that route. The opposite is eager loading (everything loads at app start).
"Lazy = aalas mein uthna โ jab zarurat ho tab uthta hai."
"Eager = subah 5 baje uthna โ chahe kaam ho ya na ho."
With lazy loading, Angular splits your code into separate JavaScript bundles (chunks). The initial bundle is small โ only what's needed for the first page. Additional chunks download on demand as the user navigates.
Lazy loading dramatically improves initial load time and Core Web Vitals.
- Eager loaded app (50 components): 5MB initial bundle โ user waits for everything to download before seeing anything
- Lazy loaded app: 500KB initial chunk, rest downloads on demand โ user sees first page in 1-2 seconds
"Hyderabad mein 40ยฐC hai โ chota bundle = kam loading time = kam garmi."
Better user experience: faster Time to Interactive (TTI), better Largest Contentful Paint (LCP), lower bounce rate. Better SEO because search engines see faster pages.
loadComponent (Angular 16+) lets you lazy load a single standalone component without any module.
// app.routes.ts
const routes: Routes = [{
path: 'admin',
loadComponent: () => import('./admin/admin.component')
.then(m => m.AdminComponent)
}];
The component MUST be standalone (standalone: true). Angular creates a separate chunk for this component that downloads only when /admin is visited.
"loadComponent = on-demand delivery โ jab order karo tab banao."
Before Angular 16, you had to create an entire NgModule just to lazy load a single component. loadComponent eliminates that boilerplate.
loadChildren lazy loads an entire set of child routes at once.
// app.routes.ts
const routes: Routes = [{
path: 'dashboard',
loadChildren: () => import('./dashboard/dashboard.routes')
.then(m => m.DASHBOARD_ROUTES)
}];
// dashboard.routes.ts
import { Routes } from '@angular/router';
import { OrdersComponent } from './orders.component';
import { ProfileComponent } from './profile.component';
export const DASHBOARD_ROUTES: Routes = [
{ path: '', component: DashboardHomeComponent },
{ path: 'orders', component: OrdersComponent },
{ path: 'profile', component: ProfileComponent },
];
"loadChildren = poora family ek saath aata hai โ ek bhi bulao toh sab aate hain."
All child routes are in the same chunk. When user visits /dashboard, the entire dashboard chunk downloads with all its child components.
By default, lazy chunks load ONLY when navigated. Preloading changes this โ it downloads lazy chunks in the background AFTER the initial bundle, so when the user clicks, it's instant.
// Preload ALL lazy chunks after initial load
provideRouter(routes, withPreloading(PreloadAllModules))
// Custom preloading โ specify which routes to preload
provideRouter(routes, withPreloading(customPreloadStrategy))
"Preloading = smart aalas โ pehle important kaam, baaki background mein load karo."
Recommended approach: Use PreloadAllModules for most apps. The initial bundle loads fast, then all other chunks preload silently in the background before the user clicks.
Key Takeaways
- โ Lazy loading loads code only when user navigates to that route (faster initial load)
- โ loadComponent lazy loads a single standalone component (Angular 16+, no module needed)
- โ loadChildren lazy loads an entire set of child routes in one chunk
- โ Preloading (withPreloading) downloads lazy chunks in background after initial load
- โ Services remain in root bundle โ only components and templates are lazy loaded
Want to track your progress?
Log in to save your place and pick up where you left off.
Progress track karna chahte ho?
Login karo apni progress save karne ke liye aur jahan chhoda tha wahan se shuru karo.
Login