Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Angular17 - Deferred Loading Using Defer Block

Tags:
Angular17 - Deferred Loading Using Defer Block

Angular 17 recently came out with several exciting updates.it has an exciting new feature called deferred loading

Lazy loading is a method that helps web apps load things like scripts only when necessary. Instead of loading everything at the start, it waits to load less important stuff until the user does something like interacting with the page or scrolling to a certain point.

Lazy loading improves the user experience by making the initial page load faster. This means users can begin using the app sooner while the less important parts load quietly in the background. It also decreases the amount of internet data needed and eases the strain on the server.

In earlier versions of Angular, we were able to load a specific part of the application later using the Router, or by using dynamic imports along with ngComponentOutlet.

Angular17 now has a @defer control block enabling lazy-loading of the content of the block. Lazy-loading also applies to the dependencies of the content of the block: all the components, directives and pipes will be lazy-loaded, too.

I will demonstrate the key aspects of lazy loading in Angular 17, such as

  • Using @defer with a logical expression
  • Using @defer with a declarative trigger condition

Using @defer with a logical expression

In the below example, I make a checkbox and bind it to the isChecked property. Initial value of isChecked is false, so at the start, the checkbox is unccheked, and the content inside the @defer block doesn't appear.

@defer with a logical expression


@defer (when isChecked) { } @placeholder { Placeholder } @error { Error } @loading(minimum 1s) { Loading... }

The @defer (when logical_expression) creates @defer block with the logical expression. I use the isChecked property as a logical expression, as it evaluates to a boolean value.

In the above example, we have four block types under the @defer block. These are-

  • An @defer block in Angular displays its content only when the isChecked turns true. Inside, there's a child component: .
  • A @placeholder block, it's rendered initially, before the @defer block is triggered
  • When the @defer block activates, Angular fetches its content from the server. While it loads, the @loading block is displayed.
  • If the loading fails, Angular shows the @error block

The use of the @placeholder, @loading and @error blocks are optional, so we can use standalone @defer blocks, too.

Let's explore this code! When we launch the app, the isChecked is false. This means the @defer block doesn't triggered, and the content of the @placeholder block is visible.

When we check the checkbox, the isChecked turns true. Angular then loads the @defer block's content. It replaces the content of the @placeholder block and shows the @loading block. I set a minimum duration for this block to appear for at least one second. There's an option to set an "after" condition, which sets the minimum time to wait before the @loading block appears. If the content from the @defer block loads within this time, Angular won't display the @loading block.

Content of the @loading block is visible for one second:

Then the content of the @defer block, the component is shown:

In the Developer tools, it's visible that after checking the box, Angular loaded a chunk of the application, which includes the content from the @defer block.

Using @defer with a declarative trigger condition

Deferred blocks can be triggered using various declarative types:

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

Below are the examples of all these:

  • @defer on interaction:Angular renders the on interaction block, when the user interacts with its @placeholder block. An interaction can be a click, touch focus, or input events:

    @defer on interaction

    @defer (on interaction) { Clicked } @placeholder { Placeholder (click on it!) }
  • @defer on idle:Angular renders the on idle block, when the browser reaches an idle state after the page has been loaded:

    @defer on idle

    @defer (on idle) { Browser has reached an idle state } @placeholder { Placeholder }
  • @defer on hover:Angular renders the on hover block, when the user hovers over its @placeholder block:

    @defer on hover

    @defer (on hover) { Hovered } @placeholder { Placeholder (hover it!) }
  • @defer on timer:The on timer block is rendered after the specified time is elapsed:

    @defer on timer(5s)

    @defer (on timer(5s)) { Visible after 5s } @placeholder { Placeholder }
  • @defer on viewport:Angular renders the on viewport block, when the placeholder enters the browser's viewport:

    @defer on viewport

    @defer (on viewport) { } @placeholder { Placeholder }
  • Prefetch:Next to a trigger condition, we can specify an additional prefetch condition.In prefetch, Angular loads the content of the @defer block, but it's not rendered, the @placeholder remains visible.Then we hover on the placeholder, and Angular renders the prefetched block.

    Prefetch

    @defer (on interaction; prefetch on hover) { } @placeholder { Placeholder (hover it, then click on it!) }

The full source code is available here:

Happy coding!! 😊



This post first appeared on Dot Net World, please read the originial post: here

Share the post

Angular17 - Deferred Loading Using Defer Block

×