Return Blog
Angular

Part 1: Discover Deferrable Views in Angular 17

Sergio Rojas
Sergio Rojas
2 min read 11 Apr, 2024
Share
Part 1: Discover Deferrable Views in Angular 17
Summarize with AI:
Prompt copied! Paste it (Cmd/Ctrl+V) in the chat. Open AI

Give your application a breather with Angular 17’s Deferrable Views! This feature allows you to postpone component loading until they are actually needed. Think of it as giving your app an “express pass” to skip the initial loading queue, making the user experience smoother right from the start.

Maximum Optimization

Why load everything at once when you can plan a casual encounter? Use @defer to invite components to the party only when the time is right. By doing so, you reduce your initial bundle size and improve key performance metrics like LCP (Largest Contentful Paint) and TTFB (Time to First Byte).

@defer {
  <large-component />
}

More Than Just Loading: State Management

Deferrable Views don’t just lazy-load components; they also manage loading states with style. The @defer blocks can include sub-blocks to handle “loading” and “error” states, as well as display “placeholders”. This prevents jarring UI shifts that could scare your users away, much like how you would avoid sudden lighting changes at a surprise party.

Best Practice: Prevent Content Layout Shifts

It is crucial to place components that might change the page structure (causing layout shifts) below the fold. This way, when they load, they won’t disrupt the user experience—like a DJ smoothly transitioning to a new track without anyone noticing.

Requirements for Effective Loading

For a dependency inside @defer blocks to be deferrable in Angular 17, it must be a standalone component and must not be referenced anywhere else outside of the @defer block. This ensures that lazy loading is truly effective and prevents inadvertent dependencies from triggering an eager load, thereby maintaining the efficiency and effectiveness of the deferrable loading mechanism.

Types of Deferrable Views

Angular 17 doesn’t just improve loading efficiency with @defer; it also offers multiple ways to handle different scenarios using Blocks and Triggers. The Blocks include:

  • @defer
  • @placeholder
  • @loading
  • @error

Each is specialized in managing distinct phases of the loading process.

As for the Triggers, we have:

  • on idle
  • on viewport
  • on interaction
  • on hover
  • on immediate
  • on timer

These allow you to control the exact moment of loading based on the user’s context and their interaction with the application.

For a more detailed analysis of each of these blocks and triggers, make sure not to miss my next post, where we will explore how each one can further optimize your Angular 17 application.