Skip to content

Commit

Permalink
fix(router): Ensure Router preloading works with lazy component and s…
Browse files Browse the repository at this point in the history
…tatic children (#49571)

The preloading strategy did not handle a `loadComponent` on a route with
a static `children`. It only preloaded children if they were also
`loadChildren` or both were not lazy loaded.

fixes #49558

PR Close #49571
  • Loading branch information
atscott committed Mar 24, 2023
1 parent 656a388 commit 978d37f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/router/src/router_preloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ export class RouterPreloader implements OnDestroy {
if ((route.loadChildren && !route._loadedRoutes && route.canLoad === undefined) ||
(route.loadComponent && !route._loadedComponent)) {
res.push(this.preloadConfig(injectorForCurrentRoute, route));
} else if (route.children || route._loadedRoutes) {
}
if (route.children || route._loadedRoutes) {
res.push(this.processRoutes(injectorForChildren, (route.children ?? route._loadedRoutes)!));
}
}
Expand Down
30 changes: 30 additions & 0 deletions packages/router/test/router_preloader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -818,5 +818,35 @@ describe('RouterPreloader', () => {
tick(3);
expect(getLoadedComponent(baseRoute)).toBeDefined();
}));

it('loads nested components', () => {
@Component({template: '', standalone: true})
class LoadedComponent {
}
lazyComponentSpy.and.returnValue(LoadedComponent);

TestBed.inject(Router).resetConfig([
{
path: 'a',
loadComponent: lazyComponentSpy,
children: [{
path: 'b',
loadComponent: lazyComponentSpy,
children: [{
path: 'c',
loadComponent: lazyComponentSpy,
children: [{
path: 'd',
loadComponent: lazyComponentSpy,
}]
}]
}]
},
]);

const preloader = TestBed.inject(RouterPreloader);
preloader.preload().subscribe(() => {});
expect(lazyComponentSpy).toHaveBeenCalledTimes(4);
});
});
});

0 comments on commit 978d37f

Please sign in to comment.