fix(router): lazy loading keeps refetching modules (#10707)
This commit is contained in:
parent
f48142e679
commit
cc6749c158
|
@ -244,10 +244,14 @@ class ApplyRedirects {
|
||||||
} else if (route.loadChildren) {
|
} else if (route.loadChildren) {
|
||||||
return runGuards(injector, route).mergeMap(shouldLoad => {
|
return runGuards(injector, route).mergeMap(shouldLoad => {
|
||||||
if (shouldLoad) {
|
if (shouldLoad) {
|
||||||
return this.configLoader.load(injector, route.loadChildren).map(r => {
|
if ((<any>route)._loadedConfig) {
|
||||||
(<any>route)._loadedConfig = r;
|
return of ((<any>route)._loadedConfig);
|
||||||
return r;
|
} else {
|
||||||
});
|
return this.configLoader.load(injector, route.loadChildren).map(r => {
|
||||||
|
(<any>route)._loadedConfig = r;
|
||||||
|
return r;
|
||||||
|
});
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
return canLoadFails(route);
|
return canLoadFails(route);
|
||||||
}
|
}
|
||||||
|
|
|
@ -406,7 +406,6 @@ export class Router {
|
||||||
})
|
})
|
||||||
.forEach((shouldActivate: boolean) => {
|
.forEach((shouldActivate: boolean) => {
|
||||||
if (!shouldActivate || id !== this.navigationId) {
|
if (!shouldActivate || id !== this.navigationId) {
|
||||||
this.routerEvents.next(new NavigationCancel(id, this.serializeUrl(url)));
|
|
||||||
navigationIsSuccessful = false;
|
navigationIsSuccessful = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -429,9 +428,14 @@ export class Router {
|
||||||
.then(
|
.then(
|
||||||
() => {
|
() => {
|
||||||
this.navigated = true;
|
this.navigated = true;
|
||||||
this.routerEvents.next(
|
if (navigationIsSuccessful) {
|
||||||
new NavigationEnd(id, this.serializeUrl(url), this.serializeUrl(appliedUrl)));
|
this.routerEvents.next(
|
||||||
resolvePromise(navigationIsSuccessful);
|
new NavigationEnd(id, this.serializeUrl(url), this.serializeUrl(appliedUrl)));
|
||||||
|
resolvePromise(true);
|
||||||
|
} else {
|
||||||
|
this.routerEvents.next(new NavigationCancel(id, this.serializeUrl(url)));
|
||||||
|
resolvePromise(false);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
e => {
|
e => {
|
||||||
this.currentRouterState = storedState;
|
this.currentRouterState = storedState;
|
||||||
|
|
|
@ -266,6 +266,32 @@ describe('applyRedirects', () => {
|
||||||
expect((<any>config[1])._loadedConfig).toBe(loadedConfig);
|
expect((<any>config[1])._loadedConfig).toBe(loadedConfig);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should load the configuration only once', () => {
|
||||||
|
const loadedConfig = new LoadedRouterConfig(
|
||||||
|
[{path: '', component: ComponentB}], <any>'stubInjector', <any>'stubFactoryResolver');
|
||||||
|
|
||||||
|
let called = false;
|
||||||
|
const loader = {
|
||||||
|
load: (injector: any, p: any) => {
|
||||||
|
if (called) throw new Error('Should not be called twice');
|
||||||
|
called = true;
|
||||||
|
return of (loadedConfig);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const config = [{path: 'a', loadChildren: 'children'}];
|
||||||
|
|
||||||
|
applyRedirects(<any>'providedInjector', <any>loader, tree('a?k1'), config).subscribe(r => {});
|
||||||
|
|
||||||
|
applyRedirects(<any>'providedInjector', <any>loader, tree('a?k2'), config)
|
||||||
|
.subscribe(
|
||||||
|
r => {
|
||||||
|
compareTrees(r, tree('a'));
|
||||||
|
expect((<any>config[0])._loadedConfig).toBe(loadedConfig);
|
||||||
|
},
|
||||||
|
(e) => { throw 'Should not reach'; });
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('empty paths', () => {
|
describe('empty paths', () => {
|
||||||
|
|
Loading…
Reference in New Issue