Slow loading on Angular (Put it into action on your Web Site) -(r) (r)

Jan 20, 2023
Illustration: The page-loading wait — why we need lazy loading in Angular.

To reduce load time and increase your overall experience for your customers, use a technique referred to by the name lazy load. It's an native Angular function that permits you to just load the most essential elements of your website application first and then load additional modules as needed.

In this blog, we'll talk about lazy loading and ways it can increase the speed of your site application.

What exactly do you mean when you say Lazy Loading?

Lazy loading is the most common method of loading for image and video files when websites host a variety of media. Instead of loading every media simultaneously, which could consume a significant amount of bandwidth, and slow down the loading time of the website, these elements are loaded as soon they are placed on the webpage is set to be scrolled into view.

Angular is a one-page web application framework that relies on JavaScript to provide a significant amount of its functions. The application's library makes use of JavaScript is able to grow in size in the event that the application expands, that can cause increasing data consumption and load time. In order to speed up the process using lazy loading. This allows you to start with loading the modules you need before delaying the loading of the other modules until the time they are necessary.

Benefits of Lazy Loading in Angular

Lazy loading has advantages to enhance the user experience on your site. They include:

  • A faster loading speed: JavaScript contains instructions for displaying your web page and processing the information. This is why it's a rendering resource that blocks any rendering. It is necessary for the browser to load the whole JavaScript before rendering your website. If you are using lazy loading in Angular it is the case that JavaScript is divided into pieces that are loaded distinct. The chunk which is loaded first has the necessary logic to make the main portion of what is the basis of the website. The webpage loads fast prior to the other modules are loaded slowly. When you decrease the size of the initial chunk, the site will is faster to load and render.
  • Use less data through dividing information into smaller chunks and process it according to the requirements it is possible to reduce the bandwidth that is consumed.
  • Conserved browser resources Because the browser loads only the chunks that are necessary that it doesn't eat the memory and CPU trying to translate and render code that aren't necessary.

Implementing Lazy Loading in Angular

In order to comply with the instructions, you'll need to have these items:

  • NodeJS installed
  • Basic understanding of the fundamentals of

Step Up Your Project

     NPM installs the following command the following way: -g @angular/cli    

You can then develop an application that is dubbed Lazy loader demo. This is how it works:

HTML0 new demo of lazy loading -- routing

This command will start the creation of a fresh Angular project that includes routing. It will be entirely in the src/app folder. The directory is home to all the source code of the application. This folder contains your main routing file, app-routing.module.ts. The folder's structure will be like this:

Screenshot: The Angular folder structure displayed in a terminal.
The structure of a folder in the context of an Angular project.

Create a Feature module using Routes

The next step is to create an feature module that will load slowly. To make the module, you need to execute this command:

ng generate module blog --route blog --module app.module

This command will generate the new module BlogModule and also generate a routing. If you open src/app/app-routing.module.ts, you will see it now looks like this:

import NgModule from '@angular/core'; import RouterModule, Routes from '@angular/router'; const routes: Routes = [ path: 'blog', loadChildren: () => import('./blog/blog.module').then(m => m.BlogModule) ]; @NgModule( imports: [RouterModule.forRoot(routes)], exports: [RouterModule] ) export class AppRoutingModule 

The most important part in lazy loading is the third line:

const routes: Routes = [ path: 'blog', loadChildren: () => import('./blog/blog.module').then(m => m.BlogModule) ]; 

The line that is used to determine the route. The blog's blog's route uses an argument referred to as"the loadChildren argument instead of the element. It is the loadChildren argument tells Angular to make use of lazy loading for the path, which is to load the modules dynamically when it has completed, and then to bring it back into the route. It's designated as a child routing similar to blog. /**. blog// is the name used for blog/** in the routing.module.ts file. Your blog module that was created appears like this:

import NgModule from '@angular/core'; import RouterModule, Routes from '@angular/router'; import BlogComponent from './blog.component'; const routes: Routes = [ path: '', component: BlogComponent ]; @NgModule( imports: [RouterModule.forChild(routes)], exports: [RouterModule] ) export class BlogRoutingModule 

It is one route that is ''. It connects to blogs and redirects towards the BlogComponent. There is the option of adding additional components and define your routes in the file.

If, for instance, you wish to add the capability of obtaining the data of a particular blog, you can create the component using this command:

ng generate component blog/detail

The result is a component which contains blog's details that is then added to the module for blogs. If you'd like an option to add blog information, all you need to do is to include it in the various routes available:

const routes: Routes = [ path: '', component: BlogComponent , path:"/:title",component: DetailComponent]; 

This adds a route that resolves for blog/:title (for example, blog/angular-tutorial). The routes listed included in the bundle is lazy loaded and are not part of the bundle when it was originally created.

Verify Lazy Loading

It is possible to determine if lazy loading is functioning by using an Ng server by observing the output. In the lower portion of the output, you'll notice these results:

Screenshot: Output of Angular's ng serve command in the terminal.
Test lazy loading using Angular's service ng.

The information above is divided into two sections: Initial Chunk Files are the ones that are loaded when the website is loading. Lazy Chunk Filesare lazy loaded. The blog module will be explained in the following illustration.

Verifying for lazy loading through Browser Network Logs

Another way to confirm lazy loading is using an option, which is known as the Network tab within Mozilla's Developer Tools panel. (On Windows, that's the command combination F12 in Chrome and Microsoft Edge, and Ctrlor ShiftI I in Firefox. If you're running Mac the option will be CommandOptionI I. OptionI I in Chrome, Firefox and Safari.)

Choose the filter. Select the JS filter, so you can only see JavaScript documents that were uploaded to the web. Once you have loaded the application, you'll be able to see this type of thing:

Screenshot: Angular JavaScript files logged in Developer Tools.
The logs for the initial JavaScript downloads can be viewed by using Developer Tools.

When you navigate to /blog, you will notice a new chunk, src_app_blog_blog_module_ts.js, is loaded. It means that your application was only requested when you clicked on that link and it is loaded slowly. The log of the network should be displayed in the format below:

Screenshot: Updated view of Angular JavaScript files logged in Developer Tools.
Downloads using lazy-loaded modules that are logged in Developer Tools.

Lazy Loading Vs Eager Loading

In order to compare the performance of the newly loaded module and see how it impacts its size, as well as load time. For this purpose, you'll develop a module to authenticate. It'll require a quick loading time, since authentication is something will require each user to perform.

Make an AuthModule using this command from the CLI

ng generate module auth --routing --module app.module

This creates the module in addition to creating those routing file. The module is also added into the app.module.ts. app.module.ts file. However, in contrast to the command we utilized to make an application the previous time that one we're using now isn't loaded with lazy routes. It makes use of making use of the "-routing" parameter instead of the full name of the parameters"route". This adds the authentication module to app.module.ts' imports array of app.module.ts:

@NgModule( declarations of the AppComponent imports: [ BrowserModule, AppRoutingModule, AuthModule [added auth module] providers: [, bootstrap AppComponent )

The addition of AuthModule in the AppModule imports array indicates that this module has been included in the first chunk files, and is included in the JavaScript bundle. If you'd like to check the validity of your request to be sure, you run the command ng serve for a second time and verify the results:

Screenshot: Angular scripts after authentication module is added.
The result from the Angular's "ng Serve" command after authentication module has been installed.

It is evident that the authentication module isn't embedded in these lazy chunks. Additionally the size of the first bundle is increasing. Its main.js file almost is doubled in size. The file has increased from 8KB to 1KB. This change isn't that significant since these components do not contain many lines of software. However, as you load these components with logic, the size of your files will expand and give the argument that you need to employ lazy loading.

Summary

  • Easy management and setting up for My Dashboard. My dashboard
  • Expert assistance 24/7
  • The most efficient Google Cloud Platform hardware and network is powered by Kubernetes for the highest capacity
  • Enterprise-level Cloudflare integration that speeds up and security
  • The global audience is reached through up to 35 data centers, as plus 275+ POPs all across the globe

This post was first seen on this site

This post was posted on here