The new @defer feature in Angular is part of the framework's enhancements to improve performance, especially in lazy loading and rendering optimization. Here’s a quick overview of the @defer feature and the @placeholder and @loading blocks.
@defer {}
Under the hood, @defer uses the IntersectionObserver API. This API allows you to observe changes in the intersection of a target element with an ancestor element or the top-level document’s viewport. By deferring the loading of components until they are about to enter the viewport, you can avoid loading resources that the user may never see, thus conserving bandwidth and processing power.
Improved Initial Load Time: Deferring components until they are needed ensures that only the most critical parts of the application are loaded initially. This reduces the initial load time and improves the perceived performance of the application, making it feel faster and more responsive. Angular will create separate bundles for the components that are deferred, thus also reducing the size of your main bundle.
Enhanced User Experience: By loading content just before it becomes visible, you can ensure a smoother and more seamless experience for users. This technique can be especially beneficial for long-scrolling pages, where loading content as the user scrolls can prevent the application from becoming sluggish.
Better Performance on Low-Power Devices: Devices with limited processing power or slow network connections can benefit significantly from deferred loading. By only loading necessary components, these devices can handle applications more efficiently, providing a better experience for users on a wide range of devices.
Here’s an example demonstrating how to use @defer in an Angular application. First, create a component that loads images. Using standalone components is a requirement for @defer.
import { Component } from "@angular/core"; @Component({ selector: "app-images", standalone: true, template: `@for(image of list; track image) { }`, }) export class ImagesComponent { list = Array(5).fill("https://placehold.co/600x400"); }
And here we are using @defer without any options.
Angular Defer Sample Application
@defer () {}
Now, looking at the generated bundle, we can see that the image component has its own chunk.
In the network tab, when the page is loaded, we can see that this bundle is now loaded at runtime.
Several options are available to enhance the user experience. In this section, we will go through some of them.
By default, the defer block will render the content when it is visible in the viewport. However, there can be delays, for example, when the components are making HTTP requests. In those scenarios, we can use the @placeholder option. The content used for the placeholder is not lazy loaded. The content in the @placeholder is shown first until the @defer block's contents are ready to render. The placeholder itself comes with an optional argument called minimum. This allows you to set the time to display the content.
Here is how this would look:
Angular Defer Sample Application
@defer () {} @placeholder (minimum 500ms) { Loading Images
}
And here is how this looks:
The @loading block is used to display some content while the content defined in the @defer block is still loading or has started to load. This is different from the @placeholder block, which will appear before the loading starts. This block comes with two optional arguments, after and minimum. Similar to the @placeholder argument, the minimum argument is used to set the time to display the content. The second argument, after, is used to define the waiting time before showing the @loading content.
Here is how this would look:
Angular Defer Sample Application
@defer () {} @loading (after 1s; minimum 500ms) { Loading Images
}
While you may not see this properly in the animated GIF, we are telling the block to wait at least 1 second before displaying the @loading content and show it for at least 500 ms.
The @defer feature in Angular is a powerful tool for enhancing performance and user experience by delaying the loading of components until they are needed. By integrating this feature with options like @placeholder and @loading, developers can create more efficient and responsive applications. As Angular continues to evolve, features like @defer will play a crucial role in optimizing resource management and improving application performance across various devices and network conditions.
The above is the detailed content of Boosting Angular Performance with @defer and Lazy Loading. For more information, please follow other related articles on the PHP Chinese website!