Optimizing Angular Applications
Published: 2021-02-12 20:35:03

<!-- wp:paragraph -->
<p>In this article you can find some selectable tips that help you  optimize you angular application, I hope you benefit from it in your projects. Lets start.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Critical CSS Inlining:</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>This is a new and cool feature. During the build process, Angular will parse the CSS selectors and try to find a match against the rendered page. Accordingly Angular will decide if that style is part of the Critical Path, and will inline those styles in the HTML. Enabling this feature will improving the <a href="https://web.dev/first-contentful-paint/">First Contentful Paint</a> metric of your page.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>You can enable the critical css inlining by adjusting the&nbsp;<code>optimization</code>&nbsp;browser builder option in angular.json file:</p>
<!-- /wp:paragraph -->

<!-- wp:enlighter/codeblock {"language":"css"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="css" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">"optimization": { 
  "styles": {
    "minify": true,
    "inlineCritical": true // enable critical css inlining
  }
}</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:paragraph -->
<p>If you are using Angular Universal to SSR your application, and want to enable Critical CSS Inlining, you need to enable it in <code>server.ts</code> file</p>
<!-- /wp:paragraph -->

<!-- wp:enlighter/codeblock {"language":"typescript"} -->
<pre class="EnlighterJSRAW" data-enlighter-language="typescript" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">  server.engine('html', ngExpressEngine({
    bootstrap: AppServerModule,
    inlineCriticalCss: true
  }));</pre>

<!-- wp:separator -->
<hr class="wp-block-separator"/>
<!-- /wp:separator -->

<!-- wp:heading -->
<h2>Font Inlining </h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>This is another way to improve the <a href="https://web.dev/first-contentful-paint/">First Contentful Paint</a>. You can enable this optimization option If you are using a google font, angular will then inline the google font in the index.html file. This will save you one extra request that the browser needs to do in order to get and render the font.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>You can enable the font inlining also from the <code>optimization</code>&nbsp;browser builder option in angular.json file:</p>
<!-- /wp:paragraph -->

<!-- wp:enlighter/codeblock -->
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">"optimization": { 
  "fonts": {
    "inline": true,// enable font inlining
  }
}</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:paragraph -->
<p>You can learn more about the Critical Css Inlining and Font Inlining here <a href="https://angular.io/guide/workspace-config#styles-optimization-options">https://angular.io/guide/workspace-config#styles-optimization-options</a>.</p>
<!-- /wp:paragraph -->

<!-- wp:separator -->
<hr class="wp-block-separator"/>
<!-- /wp:separator -->

<!-- wp:heading -->
<h2>Use OnPush Change Detection Strategy</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>This strategy prevents angular from running change detection on the entire component subtree, which leads to faster execution. You can enable OnPush change detection in the Component configuration metadata by changing the <code>changeDetection</code> property as the following:</p>
<!-- /wp:paragraph -->

<!-- wp:enlighter/codeblock -->
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">@Component({
	selector: 'my-company',
	templateUrl: './my-company.component.html',
	styleUrls: ['./my-company.component.scss'],
	changeDetection: ChangeDetectionStrategy.OnPush,
})</pre>
<!-- /wp:enlighter/codeblock -->

<!-- wp:paragraph -->
<p>This will deactivate the automatic change detection for this component and its child directives, but keep the possibility to the change detection to be explicitly invoked. The only three cases where angular run change detection on your component in this case are :</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>One of the component @output references has been changed.</li><li>Any Dom event from within the component or one of its child directives.</li><li>Explicitly triggering change detection.</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Here you find two in detail article about OnPush change detection: </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><a href="https://netbasal.com/a-comprehensive-guide-to-angular-onpush-change-detection-strategy-5bac493074a4">https://netbasal.com/a-comprehensive-guide-to-angular-onpush-change-detection-strategy-5bac493074a4</a></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><a href="https://blog.angular-university.io/onpush-change-detection-how-it-works/">https://blog.angular-university.io/onpush-change-detection-how-it-works/</a></p>
<!-- /wp:paragraph -->

<!-- wp:separator -->
<hr class="wp-block-separator"/>
<!-- /wp:separator -->

<!-- wp:heading -->
<h2>Server Side Rendering with Angular Universal</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>In a <a href="https://nichola.dev/user-experience-core-web-vitals-optimization-angular-universal/">previous article </a>I used Angular Universal to server-side render a sample web application in order to improve the user experience and boost Core Web Vitals scores.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>By default, Angular applications are rendered client-side. In other words, the browser loads all the application javascript files, bootstrap the application, made the necessary API calls to get the data to be displayed, and then render the final page. While Angular Universal will execute the application on the server-side, generate a static HTML which will be passed to the client to directly render it to the user. After that, the browser will load the javascript files and re-hydrate the application on the client-side. As a result of this, the application will be rendered much faster, and the user will be able to see the rendered page earlier.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>You can find a full details on how to enable and use Angular Universal here <a href="https://angular.io/guide/universal">https://angular.io/guide/universal</a></p>
<!-- /wp:paragraph -->

<!-- wp:separator -->
<hr class="wp-block-separator"/>
<!-- /wp:separator -->

<!-- wp:heading -->
<h2>Use Lazy Loading</h2>
<!-- /wp:heading -->

<!-- wp:heading {"level":3} -->
<h3>Modules Lazy Loading</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>By default, all your Angular code is shipped to the client in one bundle. This bundle could grow in size as you keep adding new features to your application. Luckily Angular supports the ability to lazy loading part of your application. This will improve the initial loading of your application, and so the <a href="https://web.dev/first-contentful-paint/">Largest Contentful Paint (LCP)</a>. In meanwhile Angular supports lazy loading through routing. Which means you can lazy load parts of your application as long as it has a separate routing. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>You can find more in details about how to lazy load your angular modules in the official Angular documentation <a href="https://angular.io/guide/lazy-loading-ngmodules">https://angular.io/guide/lazy-loading-ngmodules</a></p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Components Lazy Loading</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>With the new Ivy renderer, it is now possible to lazy load Angular Components, which is a great feature. However, it requires some exatra manual work to do it, especially when your component contains some child directives or child components. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Here you can find a great article on how Components Lazy Loading works <a href="https://johnpapa.net/angular-9-lazy-loading-components/">https://johnpapa.net/angular-9-lazy-loading-components/</a></p>
<!-- /wp:paragraph -->

<!-- wp:separator -->
<hr class="wp-block-separator"/>
<!-- /wp:separator -->

<!-- wp:heading -->
<h2>Use the available tools</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Finally, there are a bunch of tools you can use to measure the performance of your application, which is the first step in the optimization process. Many of these tool also provide you with suggestions on how to fix potential issues in your applications. Probably the most important of these tools are:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li><a href="https://developers.google.com/web/tools/lighthouse">Lighthouse</a>: an open-source project, shipped with chrome dev tools, with the aim to improve the quality of web pages.</li><li><a href="https://developers.google.com/speed/pagespeed/insights/">PageSpeed Insights</a>: an online tool to generate a performance report of a page on both Desktop and Mobile devices, and provide you with a wide range of suggestions on how to improve and fix the existing issues.</li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->

<!-- wp:separator -->
<hr class="wp-block-separator"/>
<!-- /wp:separator -->

<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->