I'm always excited to take on new projects and collaborate with innovative minds.
Boost Angular admin dashboard performance using lazy loading. Learn how to split modules and load them on demand to reduce initial load time and improve UX.
When building modern web applications, performance is everything—especially in admin dashboards. Admin panels are often loaded with dozens of routes, components, and modules. Without careful planning, users may experience sluggish performance, particularly during the initial load. That’s where lazy loading in Angular becomes a game-changer.
In this blog post, we’ll explore what lazy loading is, why it matters for admin dashboards, and how you can implement it effectively in Angular—with detailed code examples.
Lazy loading is a design pattern that delays the loading of a module until it is required. In Angular, this means that modules associated with specific routes are not loaded at the application start but only when the user navigates to that route.
Instead of loading everything upfront, Angular breaks the app into chunks that are loaded on demand. This drastically reduces the initial bundle size, speeding up the time it takes for your app to become interactive.
Admin dashboards typically serve internal users—often power users—who need access to a wide range of features like:
Each of these features may reside in separate modules and may not be accessed on every visit. Loading all of them upfront is inefficient. Lazy loading ensures that only the necessary modules are fetched and rendered when the user actually needs them.
Let’s go step-by-step with code.
Suppose you have an admin dashboard with a User Management and a Reports section.
Use Angular CLI to generate feature modules:
ng generate module admin/user-management --route user-management --module app.module
ng generate module admin/reports --route reports --module app.module
This will:
AppRoutingModuleThe routing file (app-routing.module.ts) will now look like:
const routes: Routes = [
{
path: 'user-management',
loadChildren: () =>
import('./admin/user-management/user-management.module').then(
(m) => m.UserManagementModule
),
},
{
path: 'reports',
loadChildren: () =>
import('./admin/reports/reports.module').then(
(m) => m.ReportsModule
),
},
{ path: '', redirectTo: '/user-management', pathMatch: 'full' },
];
Inside user-management.module.ts, make sure routing is correctly configured.
// user-management-routing.module.ts
const routes: Routes = [
{ path: '', component: UserListComponent },
{ path: ':id', component: UserDetailComponent },
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule],
})
export class UserManagementRoutingModule {}
// user-management.module.ts
@NgModule({
declarations: [UserListComponent, UserDetailComponent],
imports: [CommonModule, UserManagementRoutingModule],
})
export class UserManagementModule {}
ng generate component admin/user-management/user-list
ng generate component admin/user-management/user-detail
Example UserListComponent:
@Component({
selector: 'app-user-list',
template: `
<h2>User Management</h2>
<ul>
<li *ngFor="let user of users" (click)="goToDetail(user.id)">
{{ user.name }}
</li>
</ul>
`,
})
export class UserListComponent {
users = [
{ id: 1, name: 'John Doe' },
{ id: 2, name: 'Jane Smith' },
];
constructor(private router: Router) {}
goToDetail(id: number) {
this.router.navigate(['/user-management', id]);
}
}
If you want to lazy-load modules but also preload them in the background, you can enable preloading:
@NgModule({
imports: [
RouterModule.forRoot(routes, {
preloadingStrategy: PreloadAllModules,
}),
],
exports: [RouterModule],
})
export class AppRoutingModule {}
This ensures that routes are lazy-loaded on demand, but also pre-fetched for a smoother experience if a user is likely to visit them.
Use route guards to protect sensitive admin modules:
canActivate: [AdminGuard]
// admin.guard.ts
@Injectable({ providedIn: 'root' })
export class AdminGuard implements CanActivate {
constructor(private authService: AuthService, private router: Router) {}
canActivate(): boolean {
if (this.authService.isAdmin()) {
return true;
}
this.router.navigate(['/access-denied']);
return false;
}
}
Run:
ng build --stats-json
npx webpack-bundle-analyzer dist/browser/stats.json
This lets you visualize the bundle and confirm that lazy-loaded modules are separated correctly.
You can find the complete source code here:
👉 GitHub Repo
Feel free to fork it, experiment, or contribute improvements!
Lazy loading in Angular isn't just a nice-to-have—it's essential when dealing with feature-rich admin dashboards. It minimizes load time, improves responsiveness, and enhances scalability without sacrificing structure.
By following the steps above and using Angular’s robust module system, you can break your dashboard into focused, performant chunks. The result is a snappier and more efficient user experience for everyone who interacts with your admin tools.
Your email address will not be published. Required fields are marked *