Skip to content

Commit 9c45c32

Browse files
dimakubaAndrewKushnir
authored andcommitted
fix(router): ensure preloaded components are properly activated (#62502)
Preloaded components were not being activated in certain scenarios when preloading was enabled. This change ensures that components are correctly activated after being preloaded. PR Close #62502
1 parent 9d5e248 commit 9c45c32

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

packages/router/src/navigation_transition.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ export class NavigationTransitions {
694694
switchTap((t: NavigationTransition) => {
695695
const loadComponents = (route: ActivatedRouteSnapshot): Array<Observable<void>> => {
696696
const loaders: Array<Observable<void>> = [];
697-
if (route.routeConfig?.loadComponent && !route.routeConfig._loadedComponent) {
697+
if (route.routeConfig?.loadComponent) {
698698
const injector = getClosestRouteInjector(route) ?? this.environmentInjector;
699699
loaders.push(
700700
this.configLoader.loadComponent(injector, route.routeConfig).pipe(

packages/router/test/integration/lazy_loading.spec.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ import {
5757
createRoot,
5858
advance,
5959
} from './integration_helpers';
60+
import {getLoadedComponent} from '../../src/utils/config';
61+
import {of, delay} from 'rxjs';
6062

6163
export function lazyLoadingIntegrationSuite() {
6264
describe('lazy loading', () => {
@@ -760,6 +762,12 @@ export function lazyLoadingIntegrationSuite() {
760762
})
761763
class LazyLoadedComponent {}
762764

765+
@Component({
766+
selector: 'lazy',
767+
template: 'LazyLoadedStandaloneComponent',
768+
})
769+
class LazyLoadedStandaloneComponent {}
770+
763771
@NgModule({
764772
declarations: [LazyLoadedComponent],
765773
imports: [RouterModule.forChild([{path: 'LoadedModule2', component: LazyLoadedComponent}])],
@@ -808,6 +816,33 @@ export function lazyLoadingIntegrationSuite() {
808816
expect(secondRoutes[0].path).toEqual('LoadedModule2');
809817
});
810818

819+
it('should activate preloaded component', async () => {
820+
const router = TestBed.inject(Router);
821+
const routerPreloader = TestBed.inject(RouterPreloader);
822+
const fixture = await createRoot(router, RootCmp);
823+
824+
router.resetConfig([
825+
{path: 'blank', component: BlankCmp},
826+
{
827+
path: 'lazy',
828+
loadComponent: () => of(LazyLoadedStandaloneComponent).pipe(delay(10)),
829+
canActivate: [() => of(true).pipe(delay(20))],
830+
},
831+
]);
832+
833+
router.navigateByUrl('/blank');
834+
835+
await advance(fixture);
836+
837+
routerPreloader.preload();
838+
839+
router.navigateByUrl('/lazy');
840+
841+
await advance(fixture, 40);
842+
843+
expect(fixture.nativeElement).toHaveText('LazyLoadedStandaloneComponent');
844+
});
845+
811846
it('should not preload when canLoad is present and does not execute guard', async () => {
812847
const router = TestBed.inject(Router);
813848
const fixture = await createRoot(router, RootCmp);

0 commit comments

Comments
 (0)