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 b3789d3 commit 38b0ee2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 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
32 changes: 19 additions & 13 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} from 'rxjs/operators';
import {map, mergeMap, tap, share} from 'rxjs/operators';

import {LoadChildren, LoadedRouterConfig, Route, standardizeConfig} from './config';
import {flatten, wrapIntoObservable} from './utils/collection';
Expand All @@ -27,22 +27,28 @@ export class RouterConfigLoader {
private onLoadEndListener?: (r: Route) => 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(map((factory: NgModuleFactory<any>) => {
if (this.onLoadEndListener) {
this.onLoadEndListener(route);
}
route._loader$ = moduleFactory$.pipe(
map((factory: NgModuleFactory<any>) => {
if (this.onLoadEndListener) {
this.onLoadEndListener(route);
}

const module = factory.create(parentInjector);
const module = factory.create(parentInjector);

return new LoadedRouterConfig(
flatten(module.injector.get(ROUTES)).map(standardizeConfig), module);
}));
return new LoadedRouterConfig(
flatten(module.injector.get(ROUTES)).map(standardizeConfig), module);
}),
share()
);
}
return route._loader$;
}

private loadModuleFactory(loadChildren: LoadChildren): Observable<NgModuleFactory<any>> {
Expand Down

0 comments on commit 38b0ee2

Please sign in to comment.