Angular Preloading Lazy Modules: An In-depth Guide
What is Angular Preloading?
In Angular, preloading is a technique used to load lazy-loaded modules in the background after the application has been initially loaded. This can improve the performance and user experience of an Angular application by making necessary parts of the app available sooner than they would be with on-demand loading alone.
Why Preload Lazy Modules?
While lazy loading is effective for reducing the initial load time of an Angular application, it can lead to delays when navigating to routes that require these modules. Preloading addresses this issue by loading these modules in the background, reducing wait times when users navigate to different parts of the app.
How to Implement Preloading in Angular
Angular provides several strategies for preloading, such as NoPreloading, PreloadAllModules, and custom preloading strategies.
Basic Preloading Strategy
To implement preloading, you need to set it up in your AppRoutingModule as follows:
Configure Preloading in AppRoutingModule:
import { NgModule } from '@angular/core';
import { RouterModule, Routes, PreloadAllModules } from '@angular/router';
const routes: Routes = [
{
path: 'feature',
loadChildren: () => import('./feature/feature.module').then(m => m.FeatureModule)
}
];
@NgModule({
imports: [
RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })
],
exports: [RouterModule]
})
export class AppRoutingModule {}
Custom Preloading Strategy
Create a custom strategy if you need more control.
import { Injectable } from '@angular/core';
import { PreloadingStrategy, Route } from '@angular/router';
import { Observable, of } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class CustomPreloadingStrategy implements PreloadingStrategy {
preload(route: Route, load: () => Observable<any>): Observable<any> {
return route.data && route.data.preload ? load() : of(null);
}
}
Using the Custom Preloading Strategy:
领英推荐
import { RouterModule, Routes } from '@angular/router';
import { CustomPreloadingStrategy } from './custom-preloading.strategy';
const routes: Routes = [
{
path: 'feature',
loadChildren: () => import('./feature/feature.module').then(m => m.FeatureModule),
data: { preload: true }
}
];
@NgModule({
imports: [
RouterModule.forRoot(routes, { preloadingStrategy: CustomPreloadingStrategy })
],
exports: [RouterModule]
})
export class AppRoutingModule {}
Advantages of Preloading Lazy Modules
Disadvantages of Preloading Lazy Modules
Best Practices for Preloading Lazy Modules
Using ngx-quicklink for Preloading
ngx-quicklink is a library that improves upon the standard preloading strategy by only preloading modules associated with links currently visible in the viewport.
Setting up ngx-quicklink
Install ngx-quicklink:
npm install @angular/router ngx-quicklink
Configure ngx-quicklink in AppModule:
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { QuicklinkModule, QuicklinkStrategy } from 'ngx-quicklink';
const routes: Routes = [
{
path: 'feature',
loadChildren: () => import('./feature/feature.module').then(m => m.FeatureModule)
}
];
@NgModule({
imports: [
RouterModule.forRoot(routes, { preloadingStrategy: QuicklinkStrategy }),
QuicklinkModule
],
exports: [RouterModule, QuicklinkModule]
})
export class AppRoutingModule {}
Conclusion
Preloading lazy modules in Angular enhances application performance and user experience by ensuring necessary resources are loaded in the background. While it offers significant advantages, it’s essential to carefully consider its implementation to avoid unnecessary network load and complexity. Leveraging tools like ngx-quicklink can further optimize preloading, making it more efficient and context-aware. By following best practices and tailoring the strategy to your application's needs, you can achieve a balanced and performant Angular application.