Preloading in Angular

Preloading modules in Angular is very similar to lazy loading, with the exception that the modules will be loaded immediately after all the eager loaded modules are ready. This eliminates the possible latency when navigating to a lazy loaded module, but still has the benefit of faster initial loading of the app because the initial module(s) get loaded first.

The following covers preloading modules for Angular 2+ apps.

PreloadAllModules

The default way of doing preloading is with a preloading strategy of PreloadAllModules, meaning that all lazy-loadable modules will be preloaded. It’s very easy to implement once you have lazy loading in place.

In your app module, import PreloadAllModules along with your other router imports:

app.module.ts

import { RouterModule, Routes, PreloadAllModules }
  from '@angular/router';

And now in your NgModule’s imports, specify the preloading strategy:

app.module.ts

imports: [
  ...
  RouterModule.forRoot(routes,
    { preloadingStrategy: PreloadAllModules })
],

And there you have it! All feature modules that are lazy loadable will now be preloaded.

Custom Preloading Strategy

If you don’t want all lazy loadable modules to be preloaded, you can implement your own preloading strategy. This can be useful when some routes will only be used rarely and don’t need to be preloaded at all.

We’ll define and export a CustomPreloading class that implements PreloadingStrategy:

custom-preloading.ts

import { Observable } from 'rxjs/Observable';
import { PreloadingStrategy, Route } from '@angular/router';

export class CustomPreloading implements PreloadingStrategy {
  preload(route: Route, preload: Function): Observable<any> {
    if (route.data && route.data.preload) {
      return preload();
    } else {
      return Observable.of(null);
    }
  }
}

To implement our class we define a preload method that takes in the route information and a function to call if the module should be preloaded. The method itself should return an observable, hence why we return Observable.of(null) when a module shouldn’t be preloaded.


Now in our app module, we import our preloading strategy class, provide it, and tell RouterModule.forRoot about it:

app.module.ts

// ...
import { routes } from './routes';
import { CustomPreloading } from './custom-preloading';

@NgModule({
  ...
  imports: [
   ...
   RouterModule.forRoot(routes, { preloadingStrategy: CustomPreloading })
  ],
  providers: [CustomPreloading]
})
export class AppModule {}

Routes

Our routes can be setup with something similar to this, providing a data object with preload: true for modules that should be preloaded:

routes.ts

import { DashboardComponent } from './dashboard/dashboard.component';

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

export const routes: Routes = [
  // Dashboard is eager loaded
  { path: '', component: DashboardComponent, pathMatch: 'full' },
  // Help will be preloaded
  { path: 'help', loadChildren: './help/help.module#HelpModule', data: {preload: true}},
  // Contact will be lazy loaded
  { path: 'contact', loadChildren: './contact/contact.module#ContactModule'}
];

As with lazy loading, you can head over to your DevTools' network tab to verify that the chunks get loaded and that preloading is working.

✖ Clear

🕵 Search Results

🔎 Searching...