Sitemap
Stackademic

Stackademic is a learning hub for programmers, devs, coders, and engineers. Our goal is to democratize free coding education for the world.

Lazy Loading Without Angular Modules

4 min readAug 8, 2024

--

Press enter or click to view image in full size
Photo by Priscilla Du Preez 🇨🇦 on Unsplash

The Angular team now recommends using standalone components for all new development. This does not mean that you can no longer use modules, but one thing you do not need to use Angular modules for is lazy loading.

Lazy loading means that a component or group of components will be output to a separate JavaScript chunk and will only be loaded from the server when the user navigates to the route that needs them. This can improve initial load times by not retrieving all of the bytes needed by the application when it is initially loaded. Additionally, the user may never go to some routes and so some of those chunks may never need to be loaded.

In this post we’ll look at how to do lazy loading without Angular modules. The code samples were created with Angular 18.

Lazy Loading a Component

You can lazy load a single standalone component like this:

// app.routes.ts

import { Routes } from '@angular/router';

export const routes: Routes = [
{
path: 'about',
loadComponent: () =>
import('./about/about.component').then((m) => m.AboutComponent),
}
];

The route definition has the familiar path property, but instead of the component property we see the loadComponent property. The loadComponent property takes a function that loads a module (but not an Angular module) and then returns the AboutComponent from that (non-Angular) module.

When we run ng serve, we see from the terminal output that the AboutComponent has been placed in its own chunk:

ng serve output showing the AboutComponent in its own chunk

Next, we open the browser, navigate to the Network tab in dev tools and clear the history. When we then navigate to /about, we can see this same chunk get retrieved from the server:

Press enter or click to view image in full size
Network tab showing the AboutComponent chunk getting loaded

Note that you must navigate using routerLink to observe the lazy loading behavior. If you enter the URL in the address bar it will reload the site.

Lazy Loading a Group of Components

Most of the time you will probably want to load a group of components corresponding to a feature of your application, such as Admin. We don’t need an Angular module to group the components in the Admin feature — a folder works just as well for grouping.

Get Sean Haddock’s stories in your inbox

Join Medium for free to get updates from this writer.

You organize your application without Angular modules the same way you did with modules — you just don’t have the [feature].module or the [feature]-routing.module files. Instead, for our Admin feature we just need an admin-routes.ts file:

// admin-routes.ts

import { Route } from '@angular/router';
import { UsersComponent } from './users/users.component';
import { AdminHomeComponent } from './admin-home/admin-home.component';

export default [
{ path: 'home', component: AdminHomeComponent },
{ path: 'users', component: UsersComponent },
] satisfies Route[];

This is just a normal routes array. We are exporting the array as the default export from this file (module) and using satisfies so that we can define and export in one statement.

Back in the app.routes file, we use loadChildren to load these routes under the admin path (e.g., admin/home):

// app.routes.ts

import { Routes } from '@angular/router';

export const routes: Routes = [
{
path: 'about',
loadComponent: () =>
import('./about/about.component').then((m) => m.AboutComponent),
},
{
path: 'admin',
loadChildren: () => import('./admin/admin-routes'),
},
];

Now the terminal output from ng serve shows that the components for these two admin routes have been chunked separately:

ng serve output showing the admin route components in their own chunk

Once again, in dev tools when we navigate to /admin/home we can see this same chunk get retrieved from the server:

Press enter or click to view image in full size
Network tab showing the admin routes chunk getting loaded

Bibliography

Stackademic 🎓

Thank you for reading until the end. Before you go:

--

Sean Haddock
Sean Haddock

Written by Sean Haddock

Senior Web Domain Architect at BOKF

Stackademic
Stackademic

Published in Stackademic

Stackademic is a learning hub for programmers, devs, coders, and engineers. Our goal is to democratize free coding education for the world.