Loading on Demand: Lazy Loading

Boost your Angular app's performance. Learn to configure routes to load modules only when the user needs them, significantly reducing initial load time.

Optimization ProgressStep 1 of 7
📦 5MB
0 EXP

Welcome! Today we're optimizing an Angular app. By default, Angular bundles everything into one big file (main.js).

// app.module.ts
import { ShopModule } from './shop/shop.module'; // Eager load
// Large initial bundle size! 📦

Why Lazy Loading?

By default, Angular loads all modules eagerly. This means the user downloads the Admin dashboard code even if they are just viewing the login page. **Lazy loading** splits your application into multiple JavaScript files (chunks).

This drastically reduces the initial bundle size, resulting in faster load times.

Compiler Check

Which file size metric does Lazy Loading primarily improve?

Advanced Simulations

0 EXP

Log in to unlock these advanced training modules and test your skills.


Achievements

🏗️
Lazy Architect

Successfully configure a lazy-loaded route.

Bundle Optimizer

Correctly identify the syntax for dynamic imports.

📦
Module Master

Order the steps of the Angular loading lifecycle correctly.

Mission: Configure Lazy Route

Write a route definition object that lazy loads a `SettingsModule` when visiting the path `'settings'`.

Ng-CLI Feedback:

> Awaiting configuration...

Challenge: Construct the Route

Drag the code snippets into the correct logical order to form a valid lazy-loaded route definition.

loadChildren: () =>
path: 'admin',
import('./admin.module')
.then(m => m.AdminModule)

Challenge: Syntax Recall

Fill in the missing keywords to complete the lazy loading configuration.

path: 'user',: () => ('./user.module').(m => m.UserModule)

Consult Ng-Bot

Angular Community Hub

Code Review

Submit your routing module configuration for optimization tips from the community.

Mastering Lazy Loading in Angular

As Angular applications grow, the size of the JavaScript bundle increases. A large bundle means a slower initial download, parsing, and execution time, leading to a sluggish First Contentful Paint (FCP). **Lazy Loading** is the primary architectural pattern used to combat this.

The Concept: Feature Modules

To implement lazy loading, your application must be structured into **Feature Modules**. You cannot lazy load individual components directly; you lazy load `NgModules` (or Standalone Components in modern Angular).

Instead of importing every module into `AppModule` (which is called "Eager Loading"), you use the Router to reference them via file paths.

Deep Dive: `loadChildren` vs `component`

In your `AppRoutingModule`, the distinction is clear:

Eager Loading (Bad for Scale)

{ path: 'home', component: HomeComponent }

Requires `HomeComponent` to be imported at the top of the file, bundling it immediately.

Lazy Loading (Good)

{ 
  path: 'admin', 
  loadChildren: () => import('./admin/admin.module')
    .then(m => m.AdminModule) 
}

Uses dynamic import. Webpack creates a separate "chunk" file for this code.

Preloading Strategies

Lazy loading solves the startup time, but there is a slight delay when the user clicks the link while the network fetches the chunk. **Preloading** solves this.

By setting `preloadingStrategy: PreloadAllModules` in your router config, Angular will load the main app first, let the user interact, and then silently fetch the lazy chunks in the background.

Pro Tip: Never import a lazy-loaded module into your `AppModule` imports array. If you do, it will be eagerly loaded, defeating the entire purpose of your router configuration.

Angular Loading Glossary

Lazy Loading
A design pattern that defers the loading of non-critical resources (like code chunks) until they are actually needed.
Eager Loading
The default behavior where modules are loaded immediately upon application startup.
loadChildren
A property on the Route interface used to define a module to be loaded lazily.
Dynamic Import
The `import('path')` syntax. It returns a Promise and tells bundlers (like Webpack) to create a separate file (chunk) for the imported code.
Feature Module
An NgModule that organizes related code (components, pipes, directives) for a specific feature area, ideal for lazy loading.
PreloadAllModules
A built-in Angular strategy that preloads all lazy-loaded routes in the background after the app stabilizes.
Chunk
A separate JavaScript file generated during the build process containing the code for a lazy-loaded module.

About the Author

Author's Avatar

TodoTutorial Team

Passionate developers and educators making programming accessible to everyone.

This article was written and reviewed by our team of Angular experts, who have years of experience building enterprise-scale applications.

Verification and Updates

Last reviewed: October 2025.

We strive to keep our content accurate and up-to-date with Angular versions.

External Resources

Found an error or have a suggestion? Contact us!