AI Hints
Home›Angular Tutorial›Lazy Loading, Route Guards and Practice: Book Pages
Angular · Lesson 79 of 120

Lazy Loading

Load pages when needed.

⏱ 2 min read▶ Video at 1:34:34🎓 Free
Watch this lesson in the Angular courseWatch on YouTube ↗Full Angular course

As an app grows, so does its code. But a visitor to the home page doesn't need the code for every other page. Lazy loading downloads each page only when it's needed.

  • Apps grow
  • Not every page at once
  • Load when needed

Eager or lazy?

An eager page is part of the main bundle, downloaded at the start. A lazy page is split into its own small file, called a chunk. The browser downloads it the first time someone visits that page. So the first load is smaller, and faster.

Eager: component

  • in the main bundledownloaded at the startgood for the home page

Lazy: loadComponent

  • its own chunkdownloaded when visitedgood for other pages

loadComponent

Instead of component, a lazy route has load component. It's a function, that imports the file, and picks out the class. Import, with brackets, is what tells the build to make a separate chunk.

keyvalue
loadComponent: () => ...a function
import('./about')load the file
.then(m => m.About)pick the class
Resulta separate chunk

Lazy chunks in the build

Home is eager, so it's imported at the top. Books, and about, are lazy. Run it. The build lists the initial chunk files, which load at the start. Below them are two lazy chunk files, one for the book list, and one for about.

app.routes.tstypescript
import { Routes } from '@angular/router';
import { Home } from './home';

export const routes: Routes = [
  { path: '', component: Home },
  {
    path: 'books',
    loadComponent: () => import('./book-list').then(m => m.BookList),
  },
  {
    path: 'about',
    loadComponent: () => import('./about').then(m => m.About),
  },
];
Output
Initial chunk files | Names | Raw size | Estimated transfer size
chunk-M753IJJM.js | - | 119.36 kB | 36.03 kB
chunk-ARC23PS2.js | - | 115.73 kB | 29.42 kB
main-FQWW5NZR.js | main | 933 bytes | 933 bytes
styles-RFJL2J33.css | styles | 790 bytes | 790 bytes
| Initial total | 236.81 kB | 67.18 kB
Lazy chunk files | Names | Raw size | Estimated transfer size
chunk-ZZV4KZDS.js | book-list | 975 bytes | 975 bytes
chunk-NRNZKJPB.js | about | 344 bytes | 344 bytes
Application bundle generation complete. [0.769 seconds]
Also uses home.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-home',
  template: `<h2>Home</h2><p>Welcome to Page Turners.</p>`,
})
export class Home {}
Also uses about.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-about',
  template: `<h2>About us</h2><p>A small bookshop, since 1999.</p>`,
})
export class About {}
Also uses book-list.ts
import { Component } from '@angular/core';
import { RouterLink } from '@angular/router';
import { BOOKS } from './books';

@Component({
  selector: 'app-book-list',
  imports: [RouterLink],
  template: `
    <h2>Books</h2>
    @for (b of books; track b.id) {
      <p><a [routerLink]="['/books', b.id]">{{ b.title }}</a></p>
    }
  `,
})
export class BookList {
  books = BOOKS;
}
Also uses app.ts
import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';

@Component({
  selector: 'app-root',
  imports: [RouterOutlet],
  template: `<h1>Page Turners</h1><router-outlet />`,
})
export class App {}
Also uses books.ts
export interface Book {
  id: number;
  title: string;
  author: string;
  price: number;
  stock: number;
}

export const BOOKS: Book[] = [
  { id: 1, title: 'Dune', author: 'Frank Herbert', price: 9.99, stock: 4 },
  { id: 2, title: 'The Hobbit', author: 'J.R.R. Tolkien', price: 8.99, stock: 0 },
  { id: 3, title: 'Circe', author: 'Madeline Miller', price: 10.99, stock: 2 },
  { id: 4, title: 'Emma', author: 'Jane Austen', price: 6.99, stock: 7 },
  { id: 5, title: 'Persuasion', author: 'Jane Austen', price: 7.49, stock: 1 },
];
Also uses _mode
prod

Summary

Load component splits a page into its own chunk, downloaded only when it's visited. Next, guarding routes.

Key takeaways
  • loadComponent
  • import('./about')
  • Lazy chunk files
Next up: Route Guards — protect pages