Lazy loading feature Modules in Angular
It helps keep the Initial bundle size smaller. Let's dig in!

• Full Stack Engineer • Auth0Ambassador • Organizer at AngularHive community • Speaker • Blogger
Search for a command to run...
It helps keep the Initial bundle size smaller. Let's dig in!

• Full Stack Engineer • Auth0Ambassador • Organizer at AngularHive community • Speaker • Blogger
No comments yet. Be the first to comment.
Hello awesome peopleđź‘‹, welcome back! In this post, I am going to walk you through how to set up Tailwind CSS in an Angular project. It is going to be the easiest setup you have ever done. Come on in. This post is for projects that are using Angular ...

Hello awesome peopleđź‘‹, welcome back! This is again a super simple post about Angular, but pretty informative and you might have not read about it so far. Let's begin. What is Text Interpolation? Interpolation refers to embedding expressions or just ...

Hello awesome peopleđź‘‹, welcome back. This blog post is going to be a super simple post, and I just wanna share my TailWind CSS learning with you all. What are we going to design? A simple and minimalistic card component with 100% TailWind CSS. Code...

Hello amazing peopleđź‘‹, welcome back! Introduction If you are from a Java background, you must have used/heard about Apache Tomcat server, which is a default server any Java developer works with to deal with Web app development. The landscape has cha...

Project documentation serves as a guide for new developers in a team. Why not create one for an Angular project?

Hello awesome peopleđź‘‹, welcome back!
Angular app development is much easier with the CLI and other tools that come out of the box with Angular. We can create an app and spin that in Cloud in minutes.
But all that matters are:
Lazy loading the feature modules comes to the rescue in serving individual modules as chunks based on Routes.
Let me demonstrate the concept with a quick example.
Lazy loading is a design pattern, in which modules (respective components and assets) are loaded as and when required. Say, when the User navigates to /contact, the contact Module is loaded. This way loading of all Modules on the launch of the app on the browser is avoided, which improves the page speed.
Let's create a new project by running this command ng n ng-lazy-loading --routing
The --routing flag generates the Router module file (app-routing.module.ts) out of the box and does the basic Routes configuration.
Let's create a feature module called dashboard and load it when the route dashboard is accessed.
Run the command ng g m dashboard --route dashboard --module app.module.
This command:
dashboard module under app.module.
dashboard for dashboard module in Router module.const routes: Routes = [
{
path: 'dashboard',
loadChildren: () =>
import('./dashboard/dashboard.module').
then((m) => m.DashboardModule),
},
];
Let's create another module called contact by running this command ng g m contact --route contact --module app.module.
contact module created under App module.

You should see app-routing.module.ts updated with route configuration for contact module as well.
const routes: Routes = [
{
path: 'dashboard',
loadChildren: () =>
import('./dashboard/dashboard.module').
then((m) => m.DashboardModule),
},
{
path: 'contact',
loadChildren: () =>
import('./contact/contact.module').
then((m) => m.ContactModule),
},
];
Let's add the default route in app-routing.module.ts just in case if the routes accessed by the User not matching either dashboard or contact.
{
path: '',
redirectTo: '',
pathMatch: 'full'
}
app.component.html and replace the code with:<button style="margin: 20px; padding: 20px; color: white; background-color:'#518ded';" routerLink="">Home</button>
<button style="margin: 20px; padding:20px; color: white; background-color:'#ed6651';" routerLink="dashboard">Dashboard</button>
<button style="margin: 20px; padding:20px; color: white; background-color:'#edb451';" routerLink="contact">Contact</button>
<router-outlet></router-outlet>
developer tools (Press F12).
dashboard button, you must see the dashboard module chunk loaded in the console.
contact button, you must see the contact module chunk loaded in the console.
contact module only when the User clicks on the Contact us button on the page.Lazy loading doesn't help much for an application that has fewer pages. But it matters a lot for enterprise-level applications.
Individual modules are loaded when the user navigates to the respective routes, which improves the page speed that helps in achieving a good SEO rank from Google, Bing, and also a better User experience.
Hope you find this article useful, please feel free to share it with your friends/colleagues.
If you got any questions, feel free to post them in the comments section down below.
Happy learning!
I tweet a lot about Web development, Java, and Productivity Hacks, so feel free to follow me on Twitter at AskUdhay.
Here is one of my recent tweets: