Skip to solution
mediumFrontend

How do you optimize initial bundle size using Route Preloading with custom strategies?

690 views
01

Understand the problem

Explain customizing Route Preloading to selectively load lazy routes.

routingperformance
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Custom preloading strategies implement PreloadingStrategy and its preload() method. By checking custom route.data properties (e.g., { preload: true }), they dynamically decide whether to load lazy route bundles immediately after startup.

Solution ready — 2 min read

Classified // press E to declassify

04

Read the code

Implementing a custom preloading strategy based on route data flags
import { Injectable } from '@angular/core';
import { PreloadingStrategy, Route } from '@angular/router';
import { Observable, of } from 'rxjs';

@Injectable({ providedIn: 'root' })
export class CustomPreloadStrategy implements PreloadingStrategy {
  preload(route: Route, fn: () => Observable<any>): Observable<any> {
    const shouldPreload = route.data && route.data['preload'] === true;
    return shouldPreload ? fn() : of(null);
  }
}
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 41 of 121 decoded in the Angular track. One more won't hurt.

Back to track