Skip to content

Commit 978d37f

Browse files
committed
fix(router): Ensure Router preloading works with lazy component and static 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
1 parent 656a388 commit 978d37f

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

packages/router/src/router_preloader.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ export class RouterPreloader implements OnDestroy {
121121
if ((route.loadChildren && !route._loadedRoutes && route.canLoad === undefined) ||
122122
(route.loadComponent && !route._loadedComponent)) {
123123
res.push(this.preloadConfig(injectorForCurrentRoute, route));
124-
} else if (route.children || route._loadedRoutes) {
124+
}
125+
if (route.children || route._loadedRoutes) {
125126
res.push(this.processRoutes(injectorForChildren, (route.children ?? route._loadedRoutes)!));
126127
}
127128
}

packages/router/test/router_preloader.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,5 +818,35 @@ describe('RouterPreloader', () => {
818818
tick(3);
819819
expect(getLoadedComponent(baseRoute)).toBeDefined();
820820
}));
821+
822+
it('loads nested components', () => {
823+
@Component({template: '', standalone: true})
824+
class LoadedComponent {
825+
}
826+
lazyComponentSpy.and.returnValue(LoadedComponent);
827+
828+
TestBed.inject(Router).resetConfig([
829+
{
830+
path: 'a',
831+
loadComponent: lazyComponentSpy,
832+
children: [{
833+
path: 'b',
834+
loadComponent: lazyComponentSpy,
835+
children: [{
836+
path: 'c',
837+
loadComponent: lazyComponentSpy,
838+
children: [{
839+
path: 'd',
840+
loadComponent: lazyComponentSpy,
841+
}]
842+
}]
843+
}]
844+
},
845+
]);
846+
847+
const preloader = TestBed.inject(RouterPreloader);
848+
preloader.preload().subscribe(() => {});
849+
expect(lazyComponentSpy).toHaveBeenCalledTimes(4);
850+
});
821851
});
822852
});

0 commit comments

Comments
 (0)