Back to results for “Performance”
Angular › Performance & Lazy Loading

How would you use Angular's built in deferred loading, the at defer block, to improve performance further?

Advanced

Answer

The at defer block lets you delay loading and rendering a piece of a template until a specific trigger happens, such as the element becoming visible on screen, the user interacting with a nearby element, or after the browser becomes idle, reducing the amount of work done during the initial page load.
@Component({
  selector: 'app-product-page',
  template: `
    <app-product-details [product]="product" />
    @defer (on viewport) {
      <app-related-products />
    } @placeholder {
      <p>Loading recommendations</p>
    }
  `
})
export class ProductPageComponent {}
Real-world example A product page loads its main details immediately, but defers loading a lower priority related products section until it actually scrolls into view, speeding up the initial page load noticeably.

Common follow-ups: What other trigger conditions besides viewport does the at defer block support?;How does at defer compare to manually lazy loading a component with React.lazy or similar in other frameworks?