What is Deferrable Views in angular 17?

Priti Jha
4 min readNov 8, 2023

--

Sometimes in app development, you end up with a lot of components that you need to reference in your app, but some of those don’t need to be loaded right away for various reasons.

Maybe they are below the visible fold or are heavy components that aren’t interacted with until later. In that case, we can load some of those resources later with deferrable views.

Add a @defer block around the comments component

In your app, the blog post page has a comment component after the post details.

Wrap the comment component with a @defer block to defer load it.

@defer {
<comments />
}

The code above is an example of how to use a basic @defer block. By default @defer will load the comments component when the browser is idle.

Add a placeholder

Add a @placeholder block to the @defer block. The @placeholder block is where you put html that will show before the deferred loading starts. The content in @placeholder blocks is eagerly loaded.

@defer {
<comments />
} @placeholder {
<p>Future comments</p>
}

Add a loading block

Add a @loading block to the @defer block. The @loading block is where you put html that will show while the deferred content is actively being fetched, but hasn't finished yet. The content in @loading blocks is eagerly loaded.

@defer {
<comments />
} @placeholder {
<p>Future comments</p>
} @loading {
<p>Loading comments...</p>
}

Add a minimum duration

Both @placeholder and @loading sections have optional parameters to prevent flickering from occuring when loading happens quickly. @placeholder has minimum and @loading has minimum and after. Add a minimum duration to the @loading block so it will be rendered for at least 2 seconds.

@defer {
<comments />
} @placeholder {
<p>Future comments</p>
} @loading (minimum 2s) {
<p>Loading comments...</p>
}

Add a viewport trigger

Deferrable views have a number of trigger options. Add a viewport trigger so the content will defer load once it enters the viewport.

@defer (on viewport) {
<comments />
}

Add content

A viewport trigger is best used when you’re deferring content that’s far enough down the page that it needs to be scrolled to see. So let’s add some content to our blog post. You can either write your own, or you can copy the content below and put it inside the <article> element.

<article>
<p>and this is why. Angular has the coolest deferrable view feature that makes defer loading content the easiest and most ergonomic it could possibly be. The Angular community is also filled with amazing contributors and experts that create excellent content. The community is welcoming and friendly, and it really is the best community out there.</p>
<p>I can't express enough how much I enjoy working with Angular. It offers the best developer experience I've ever had. I love that the Angular team puts their developers first and takes care to make us very happy. They genuinely want Angular to be the best framework it can be, and they're doing such an amazing job at it, too. This statement comes from my heart and is not at all copied and pasted. In fact, I think I</p>
<p>Angular is my favorite framework, and this is why. Angular has the coolest deferrable view feature that makes defer loading content the easiest and most ergonomic it could possibly be. The Angular community is also filled with amazing contributors and experts that create excellent content. The community is welcoming and friendly, and it really is the best community out there.</p>
<p>It offers the best developer experience I've ever had. I love that the Angular team puts their developers first and takes care to make us very happy. They genuinely want Angular to be the best framework it can be, and they're doing such an amazing job at it, too. This statement comes from my heart and is not at all copied and pasted. In fact, I think I'll say these exact same things again a few times.</p>
<p>Angular is my favorite framework, and this is why. Angular has the coolest deferrable view feature that makes defer loading content the easiest and most ergonomic it could possibly be. The Angular community is also filled with amazing contributors and experts that create excellent content. The community is welcoming and friendly, and it really is the best community out there.</p>
<p>I can't express enough how much I enjoy working with Angular. It offers the best developer experience I've ever had. I love that the Angular team puts their developers first and takes care to make us very happy. They genuinely want Angular to be the best framework it can be, and they're doing such an amazing job at it, too. This statement comes from my heart and is not at all copied and pasted.</p>
</article>

Once you’ve added this code, now scroll down to see the deferred content load once you scroll it into the viewport.

--

--

Priti Jha
Priti Jha

Written by Priti Jha

Senior front-end developer writing about Angular 8 , Ionic Framework ,Javascript, HTML, Css and all front end technology.