DEV Community

Discussion on: Predictive Preloading Strategy for Your Angular Bundles

john_papa profile image
John Papa Author

I'm not sure I understand, but let me try to see if I follow your question. Angular has the interface PreloadingStrategy which we can implement in a class. This code implements it with export class OnDemandPreloadStrategy implements PreloadingStrategy. So the OnDemandPreloadStrategy must implement the preload method in that interface, which it does with

  preload(route: Route, load: () => Observable<any>): Observable<any> {
    return this.preloadOnDemand$.pipe(
      mergeMap(preloadOptions => {
        const shouldPreload = this.preloadCheck(route, preloadOptions);
        return shouldPreload ? load() : EMPTY;

Then to use this strategy we must specify a class that implements the strategy, so in the router module we specify

RouterModule.forRoot(routes, {
      preloadingStrategy: OnDemandPreloadStrategy

Ah - I think I see your point ... that last line of code in the article had a typo that didn't match my code. I just fixed it to point to the strategy. Thanks!

Thread Thread
char_greenman profile image
Commando Charlie

There we go, that would be it. Thank you.