Skip to content

Commit 8f708b5

Browse files
zzmpAndrewKushnir
authored andcommitted
fix(router): defer loading of wildcard module until needed (angular#38348)
Defer loading the wildcard module so that it is not loaded until subscribed to. This fixes an issue where it was being eagerly loaded. As an example, wildcard module loading should only occur after all other potential matches have been exhausted. A test case for this was also added to demonstrate the fix. Fixes angular#25494 PR Close angular#38348
1 parent e34c33c commit 8f708b5

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

packages/router/src/apply_redirects.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import {Injector, NgModuleRef} from '@angular/core';
10-
import {EmptyError, Observable, Observer, of} from 'rxjs';
10+
import {defer, EmptyError, Observable, Observer, of} from 'rxjs';
1111
import {catchError, concatAll, first, map, mergeMap, tap} from 'rxjs/operators';
1212

1313
import {LoadedRouterConfig, Route, Routes} from './config';
@@ -247,11 +247,12 @@ class ApplyRedirects {
247247
segments: UrlSegment[]): Observable<UrlSegmentGroup> {
248248
if (route.path === '**') {
249249
if (route.loadChildren) {
250-
return this.configLoader.load(ngModule.injector, route)
251-
.pipe(map((cfg: LoadedRouterConfig) => {
252-
route._loadedConfig = cfg;
253-
return new UrlSegmentGroup(segments, {});
254-
}));
250+
return defer(
251+
() => this.configLoader.load(ngModule.injector, route)
252+
.pipe(map((cfg: LoadedRouterConfig) => {
253+
route._loadedConfig = cfg;
254+
return new UrlSegmentGroup(segments, {});
255+
})));
255256
}
256257

257258
return of(new UrlSegmentGroup(segments, {}));

packages/router/test/apply_redirects.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import {NgModuleRef} from '@angular/core';
1010
import {TestBed} from '@angular/core/testing';
1111
import {Observable, of} from 'rxjs';
12+
import {delay} from 'rxjs/operators';
1213

1314
import {applyRedirects} from '../src/apply_redirects';
1415
import {LoadedRouterConfig, Route, Routes} from '../src/config';
@@ -435,6 +436,25 @@ describe('applyRedirects', () => {
435436
});
436437
});
437438

439+
it('should not load the configuration of a wildcard route if there is a match', () => {
440+
const loadedConfig = new LoadedRouterConfig([{path: '', component: ComponentB}], testModule);
441+
442+
const loader = jasmine.createSpyObj('loader', ['load']);
443+
loader.load.and.returnValue(of(loadedConfig).pipe(delay(0)));
444+
445+
const config: Routes = [
446+
{path: '', loadChildren: 'matchChildren'},
447+
{path: '**', loadChildren: 'children'},
448+
];
449+
450+
applyRedirects(testModule.injector, <any>loader, serializer, tree(''), config).forEach(r => {
451+
expect(loader.load.calls.count()).toEqual(1);
452+
expect(loader.load.calls.first().args).not.toContain(jasmine.objectContaining({
453+
loadChildren: 'children'
454+
}));
455+
});
456+
});
457+
438458
it('should load the configuration after a local redirect from a wildcard route', () => {
439459
const loadedConfig = new LoadedRouterConfig([{path: '', component: ComponentB}], testModule);
440460

0 commit comments

Comments
 (0)