Skip to content

Commit 660444f

Browse files
crisbetoatscott
authored andcommitted
fix(router): attempt to resolve component resources in JIT mode (#63062)
In #62758 we started loading the component resources during bootstrap in JIT mode to ensure that they're in place by the time we create the component. This won't work for lazy-loaded components in the router, because they don't exist at bootstrap time. These changes add similar logic when the router loads a component. PR Close #63062
1 parent 3b214d2 commit 660444f

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

packages/core/test/bundling/router/bundle.golden_symbols.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,7 @@
903903
"matchWithChecks",
904904
"materializeViewResults",
905905
"matrixParamsMatch",
906+
"maybeResolveResources",
906907
"maybeReturnReactiveLViewConsumer",
907908
"maybeSchedule",
908909
"maybeUnwrapDefaultExport",

packages/router/src/router_config_loader.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ import {
1616
NgModuleFactory,
1717
runInInjectionContext,
1818
Type,
19+
ɵresolveComponentResources as resolveComponentResources,
1920
} from '@angular/core';
2021
import {ConnectableObservable, from, Observable, of, Subject} from 'rxjs';
21-
import {finalize, map, mergeMap, refCount, tap} from 'rxjs/operators';
22+
import {finalize, map, mergeMap, refCount, switchMap, tap} from 'rxjs/operators';
2223

2324
import {DefaultExport, LoadedRouterConfig, Route, Routes} from './models';
2425
import {wrapIntoObservable} from './utils/collection';
@@ -61,6 +62,7 @@ export class RouterConfigLoader {
6162
runInInjectionContext(injector, () => route.loadComponent!()),
6263
).pipe(
6364
map(maybeUnwrapDefaultExport),
65+
switchMap(maybeResolveResources),
6466
tap((component) => {
6567
if (this.onLoadEndListener) {
6668
this.onLoadEndListener(route);
@@ -130,6 +132,7 @@ export function loadChildren(
130132
runInInjectionContext(parentInjector, () => route.loadChildren!()),
131133
).pipe(
132134
map(maybeUnwrapDefaultExport),
135+
switchMap(maybeResolveResources),
133136
mergeMap((t) => {
134137
if (t instanceof NgModuleFactory || Array.isArray(t)) {
135138
return of(t);
@@ -177,3 +180,19 @@ function maybeUnwrapDefaultExport<T>(input: T | DefaultExport<T>): T {
177180
// subject to property renaming, so we reference it with bracket access.
178181
return isWrappedDefaultExport(input) ? input['default'] : input;
179182
}
183+
184+
function maybeResolveResources<T>(value: T): Promise<T> | Observable<T> {
185+
// In JIT mode we usually resolve the resources of components on bootstrap, however
186+
// that won't have happened for lazy-loaded components. Attempt to load any pending
187+
// resources again here.
188+
if ((typeof ngJitMode === 'undefined' || ngJitMode) && typeof fetch === 'function') {
189+
return resolveComponentResources(fetch)
190+
.catch((error) => {
191+
console.error(error);
192+
return Promise.resolve();
193+
})
194+
.then(() => value);
195+
}
196+
197+
return of(value);
198+
}

0 commit comments

Comments
 (0)