Preloading in Angular


While this tutorial has content that we believe is of great benefit to our community, we have not yet tested or edited it to ensure you have an error-free learning experience. It's on our list, and we're working on it! You can help us out by using the "report an issue" button at the bottom of the tutorial.

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.


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:

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

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

imports: [
    { 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:

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

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:

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


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

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

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

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.

Creative Commons License