fix(router): fix lazy loading triggered by redirects from wildcard routes

Closes #12183
This commit is contained in:
vsavkin 2016-10-19 12:23:00 -07:00 committed by Alex Rickabaugh
parent 8b9ab44eee
commit 5ae6915600
3 changed files with 63 additions and 5 deletions

View File

@ -159,7 +159,6 @@ class ApplyRedirects {
if (getOutlet(route) !== outlet) return noMatch(segmentGroup); if (getOutlet(route) !== outlet) return noMatch(segmentGroup);
if (route.redirectTo !== undefined && !(allowRedirects && this.allowRedirects)) if (route.redirectTo !== undefined && !(allowRedirects && this.allowRedirects))
return noMatch(segmentGroup); return noMatch(segmentGroup);
if (route.redirectTo === undefined) { if (route.redirectTo === undefined) {
return this.matchSegmentAgainstRoute(injector, segmentGroup, route, paths); return this.matchSegmentAgainstRoute(injector, segmentGroup, route, paths);
} else { } else {
@ -172,20 +171,23 @@ class ApplyRedirects {
injector: Injector, segmentGroup: UrlSegmentGroup, routes: Route[], route: Route, injector: Injector, segmentGroup: UrlSegmentGroup, routes: Route[], route: Route,
segments: UrlSegment[], outlet: string): Observable<UrlSegmentGroup> { segments: UrlSegment[], outlet: string): Observable<UrlSegmentGroup> {
if (route.path === '**') { if (route.path === '**') {
return this.expandWildCardWithParamsAgainstRouteUsingRedirect(route); return this.expandWildCardWithParamsAgainstRouteUsingRedirect(
injector, routes, route, outlet);
} else { } else {
return this.expandRegularSegmentAgainstRouteUsingRedirect( return this.expandRegularSegmentAgainstRouteUsingRedirect(
injector, segmentGroup, routes, route, segments, outlet); injector, segmentGroup, routes, route, segments, outlet);
} }
} }
private expandWildCardWithParamsAgainstRouteUsingRedirect(route: Route): private expandWildCardWithParamsAgainstRouteUsingRedirect(
Observable<UrlSegmentGroup> { injector: Injector, routes: Route[], route: Route,
outlet: string): Observable<UrlSegmentGroup> {
const newSegments = applyRedirectCommands([], route.redirectTo, {}); const newSegments = applyRedirectCommands([], route.redirectTo, {});
if (route.redirectTo.startsWith('/')) { if (route.redirectTo.startsWith('/')) {
return absoluteRedirect(newSegments); return absoluteRedirect(newSegments);
} else { } else {
return of (new UrlSegmentGroup(newSegments, {})); const group = new UrlSegmentGroup(newSegments, {});
return this.expandSegment(injector, group, routes, newSegments, outlet, false);
} }
} }

View File

@ -305,6 +305,34 @@ describe('applyRedirects', () => {
expect((<any>config[0])._loadedConfig).toBe(loadedConfig); expect((<any>config[0])._loadedConfig).toBe(loadedConfig);
}); });
}); });
it('should load the configuration after a local redirect from a wildcard route', () => {
const loadedConfig = new LoadedRouterConfig(
[{path: '', component: ComponentB}], <any>'stubInjector', <any>'stubFactoryResolver');
const loader = {load: (injector: any, p: any) => of (loadedConfig)};
const config =
[{path: 'not-found', loadChildren: 'children'}, {path: '**', redirectTo: 'not-found'}];
applyRedirects(<any>'providedInjector', <any>loader, tree('xyz'), config).forEach(r => {
expect((<any>config[0])._loadedConfig).toBe(loadedConfig);
});
});
it('should load the configuration after an absolute redirect from a wildcard route', () => {
const loadedConfig = new LoadedRouterConfig(
[{path: '', component: ComponentB}], <any>'stubInjector', <any>'stubFactoryResolver');
const loader = {load: (injector: any, p: any) => of (loadedConfig)};
const config =
[{path: 'not-found', loadChildren: 'children'}, {path: '**', redirectTo: '/not-found'}];
applyRedirects(<any>'providedInjector', <any>loader, tree('xyz'), config).forEach(r => {
expect((<any>config[0])._loadedConfig).toBe(loadedConfig);
});
});
}); });
describe('empty paths', () => { describe('empty paths', () => {

View File

@ -1562,6 +1562,7 @@ describe('Integration', () => {
.toEqual( .toEqual(
`RouterModule.forRoot() called twice. Lazy loaded modules should use RouterModule.forChild() instead.`); `RouterModule.forRoot() called twice. Lazy loaded modules should use RouterModule.forChild() instead.`);
}))); })));
it('should combine routes from multiple modules into a single configuration', it('should combine routes from multiple modules into a single configuration',
fakeAsync(inject( fakeAsync(inject(
[Router, Location, NgModuleFactoryLoader], [Router, Location, NgModuleFactoryLoader],
@ -1705,6 +1706,33 @@ describe('Integration', () => {
[[NavigationStart, '/lazy/loaded'], [NavigationError, '/lazy/loaded']]); [[NavigationStart, '/lazy/loaded'], [NavigationError, '/lazy/loaded']]);
}))); })));
it('should work with complex redirect rules',
fakeAsync(inject(
[Router, Location, NgModuleFactoryLoader],
(router: Router, location: Location, loader: SpyNgModuleFactoryLoader) => {
@Component({selector: 'lazy', template: 'lazy-loaded'})
class LazyLoadedComponent {
}
@NgModule({
declarations: [LazyLoadedComponent],
imports: [RouterModule.forChild([{path: 'loaded', component: LazyLoadedComponent}])],
})
class LoadedModule {
}
loader.stubbedModules = {lazy: LoadedModule};
const fixture = createRoot(router, RootCmp);
router.resetConfig(
[{path: 'lazy', loadChildren: 'lazy'}, {path: '**', redirectTo: 'lazy'}]);
router.navigateByUrl('/lazy/loaded');
advance(fixture);
expect(location.path()).toEqual('/lazy/loaded');
})));
describe('preloading', () => { describe('preloading', () => {
beforeEach(() => { beforeEach(() => {
TestBed.configureTestingModule( TestBed.configureTestingModule(