From cc6749c15877f98ffb5be18806c2126b29b71e8b Mon Sep 17 00:00:00 2001 From: Victor Savkin Date: Thu, 11 Aug 2016 22:59:53 -0700 Subject: [PATCH] fix(router): lazy loading keeps refetching modules (#10707) --- .../@angular/router/src/apply_redirects.ts | 12 ++++++--- modules/@angular/router/src/router.ts | 12 ++++++--- .../router/test/apply_redirects.spec.ts | 26 +++++++++++++++++++ 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/modules/@angular/router/src/apply_redirects.ts b/modules/@angular/router/src/apply_redirects.ts index 557630e72b..23e6aae07d 100644 --- a/modules/@angular/router/src/apply_redirects.ts +++ b/modules/@angular/router/src/apply_redirects.ts @@ -244,10 +244,14 @@ class ApplyRedirects { } else if (route.loadChildren) { return runGuards(injector, route).mergeMap(shouldLoad => { if (shouldLoad) { - return this.configLoader.load(injector, route.loadChildren).map(r => { - (route)._loadedConfig = r; - return r; - }); + if ((route)._loadedConfig) { + return of ((route)._loadedConfig); + } else { + return this.configLoader.load(injector, route.loadChildren).map(r => { + (route)._loadedConfig = r; + return r; + }); + } } else { return canLoadFails(route); } diff --git a/modules/@angular/router/src/router.ts b/modules/@angular/router/src/router.ts index 8a8b2954c4..73913066fc 100644 --- a/modules/@angular/router/src/router.ts +++ b/modules/@angular/router/src/router.ts @@ -406,7 +406,6 @@ export class Router { }) .forEach((shouldActivate: boolean) => { if (!shouldActivate || id !== this.navigationId) { - this.routerEvents.next(new NavigationCancel(id, this.serializeUrl(url))); navigationIsSuccessful = false; return; } @@ -429,9 +428,14 @@ export class Router { .then( () => { this.navigated = true; - this.routerEvents.next( - new NavigationEnd(id, this.serializeUrl(url), this.serializeUrl(appliedUrl))); - resolvePromise(navigationIsSuccessful); + if (navigationIsSuccessful) { + this.routerEvents.next( + new NavigationEnd(id, this.serializeUrl(url), this.serializeUrl(appliedUrl))); + resolvePromise(true); + } else { + this.routerEvents.next(new NavigationCancel(id, this.serializeUrl(url))); + resolvePromise(false); + } }, e => { this.currentRouterState = storedState; diff --git a/modules/@angular/router/test/apply_redirects.spec.ts b/modules/@angular/router/test/apply_redirects.spec.ts index 6ee3938b96..3dc1d49c79 100644 --- a/modules/@angular/router/test/apply_redirects.spec.ts +++ b/modules/@angular/router/test/apply_redirects.spec.ts @@ -266,6 +266,32 @@ describe('applyRedirects', () => { expect((config[1])._loadedConfig).toBe(loadedConfig); }); }); + + it('should load the configuration only once', () => { + const loadedConfig = new LoadedRouterConfig( + [{path: '', component: ComponentB}], 'stubInjector', '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('providedInjector', loader, tree('a?k1'), config).subscribe(r => {}); + + applyRedirects('providedInjector', loader, tree('a?k2'), config) + .subscribe( + r => { + compareTrees(r, tree('a')); + expect((config[0])._loadedConfig).toBe(loadedConfig); + }, + (e) => { throw 'Should not reach'; }); + }); }); describe('empty paths', () => {