Skip to content

Commit

Permalink
fix(router): ensure that a route module is only loaded once
Browse files Browse the repository at this point in the history
Store an observable on the route for the currently outstanding module
load factory.

The replaces the fix angular#26557 as that allowed overlapping requests to still
load the module twice.

Fixes angular#26557 angular#22842
  • Loading branch information
pgammans committed Apr 22, 2020
1 parent f58324d commit 9031c20
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
6 changes: 6 additions & 0 deletions packages/router/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,12 @@ export interface Route {
* @internal
*/
_loadedConfig?: LoadedRouterConfig;
/**
* Filled for routes with `loadChildren` during load
* @internal
*/
_loader$?: Observable<LoadedRouterConfig>;

}

export class LoadedRouterConfig {
Expand Down
19 changes: 12 additions & 7 deletions packages/router/src/router_config_loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import {Compiler, InjectionToken, Injector, NgModuleFactory, NgModuleFactoryLoader} from '@angular/core';
import {from, Observable, of} from 'rxjs';
import {map, mergeMap, tap} from 'rxjs/operators';
import {map, mergeMap, tap, share} from 'rxjs/operators';

import {LoadChildren, LoadedRouterConfig, Route, Routes, standardizeConfig} from './config';
import {flatten, wrapIntoObservable} from './utils/collection';
Expand All @@ -28,13 +28,14 @@ export class RouterConfigLoader {
private onReadyListener?: (r: Route, routes: Routes) => void) {}

load(parentInjector: Injector, route: Route): Observable<LoadedRouterConfig> {
if (this.onLoadStartListener) {
this.onLoadStartListener(route);
}
if (!route._loader$) {
if (this.onLoadStartListener) {
this.onLoadStartListener(route);
}

const moduleFactory$ = this.loadModuleFactory(route.loadChildren!);
const moduleFactory$ = this.loadModuleFactory(route.loadChildren!);

return moduleFactory$.pipe(
route._loader$ = moduleFactory$.pipe(
map((factory: NgModuleFactory<any>) => {
if (this.onLoadEndListener) {
this.onLoadEndListener(route);
Expand All @@ -45,7 +46,11 @@ export class RouterConfigLoader {
return new LoadedRouterConfig(
flatten(module.injector.get(ROUTES)).map(standardizeConfig), module);
}),
tap((cfg) => this.postLoadRouteUpdate(route, cfg)));
tap((cfg) => this.postLoadRouteUpdate(route, cfg)),
share()
);
}
return route._loader$;
}

private postLoadRouteUpdate(route: Route, cfg: LoadedRouterConfig): void {
Expand Down

0 comments on commit 9031c20

Please sign in to comment.