fix(router): rerun resolvers when url changes

Closes #12603
This commit is contained in:
vsavkin 2016-10-28 15:17:00 -07:00
parent 091c390032
commit fe47e6b783
6 changed files with 67 additions and 9 deletions

View File

@ -30,11 +30,11 @@ import {RouterOutlet} from './directives/router_outlet';
import {recognize} from './recognize'; import {recognize} from './recognize';
import {LoadedRouterConfig, RouterConfigLoader} from './router_config_loader'; import {LoadedRouterConfig, RouterConfigLoader} from './router_config_loader';
import {RouterOutletMap} from './router_outlet_map'; import {RouterOutletMap} from './router_outlet_map';
import {ActivatedRoute, ActivatedRouteSnapshot, RouterState, RouterStateSnapshot, advanceActivatedRoute, createEmptyState, inheritedParamsDataResolve} from './router_state'; import {ActivatedRoute, ActivatedRouteSnapshot, RouterState, RouterStateSnapshot, advanceActivatedRoute, createEmptyState, equalParamsAndUrlSegments, inheritedParamsDataResolve} from './router_state';
import {NavigationCancelingError, PRIMARY_OUTLET, Params} from './shared'; import {NavigationCancelingError, PRIMARY_OUTLET, Params} from './shared';
import {DefaultUrlHandlingStrategy, UrlHandlingStrategy} from './url_handling_strategy'; import {DefaultUrlHandlingStrategy, UrlHandlingStrategy} from './url_handling_strategy';
import {UrlSerializer, UrlTree, containsTree, createEmptyUrlTree} from './url_tree'; import {UrlSerializer, UrlTree, containsTree, createEmptyUrlTree} from './url_tree';
import {andObservables, forEach, merge, shallowEqual, waitForMap, wrapIntoObservable} from './utils/collection'; import {andObservables, forEach, merge, waitForMap, wrapIntoObservable} from './utils/collection';
import {TreeNode} from './utils/tree'; import {TreeNode} from './utils/tree';
declare var Zone: any; declare var Zone: any;
@ -830,7 +830,7 @@ export class PreActivation {
// reusing the node // reusing the node
if (curr && future._routeConfig === curr._routeConfig) { if (curr && future._routeConfig === curr._routeConfig) {
if (!shallowEqual(future.params, curr.params)) { if (!equalParamsAndUrlSegments(future, curr)) {
this.checks.push(new CanDeactivate(outlet.component, curr), new CanActivate(futurePath)); this.checks.push(new CanDeactivate(outlet.component, curr), new CanActivate(futurePath));
} else { } else {
// we need to set the data // we need to set the data

View File

@ -83,8 +83,7 @@ export class RouterPreloader {
setUpPreloading(): void { setUpPreloading(): void {
const navigations = filter.call(this.router.events, (e: any) => e instanceof NavigationEnd); const navigations = filter.call(this.router.events, (e: any) => e instanceof NavigationEnd);
this.subscription = this.subscription = concatMap.call(navigations, () => this.preload()).subscribe((v: any) => {});
concatMap.call(navigations, () => this.preload()).subscribe((v: any) => {});
} }
preload(): Observable<any> { return this.processRoutes(this.injector, this.router.config); } preload(): Observable<any> { return this.processRoutes(this.injector, this.router.config); }

View File

@ -12,7 +12,7 @@ import {Observable} from 'rxjs/Observable';
import {Data, ResolveData, Route} from './config'; import {Data, ResolveData, Route} from './config';
import {PRIMARY_OUTLET, Params} from './shared'; import {PRIMARY_OUTLET, Params} from './shared';
import {UrlSegment, UrlSegmentGroup, UrlTree} from './url_tree'; import {UrlSegment, UrlSegmentGroup, UrlTree, equalSegments} from './url_tree';
import {merge, shallowEqual, shallowEqualArrays} from './utils/collection'; import {merge, shallowEqual, shallowEqualArrays} from './utils/collection';
import {Tree, TreeNode} from './utils/tree'; import {Tree, TreeNode} from './utils/tree';
@ -448,3 +448,9 @@ export function advanceActivatedRoute(route: ActivatedRoute): void {
(<any>route.data).next(route._futureSnapshot.data); (<any>route.data).next(route._futureSnapshot.data);
} }
} }
export function equalParamsAndUrlSegments(
a: ActivatedRouteSnapshot, b: ActivatedRouteSnapshot): boolean {
return shallowEqual(a.params, b.params) && equalSegments(a.url, b.url);
}

View File

@ -41,7 +41,7 @@ describe('Integration', () => {
expect(location.path()).toEqual('/simple'); expect(location.path()).toEqual('/simple');
}))); })));
describe('should execute navigations serialy', () => { describe('should execute navigations serially', () => {
let log: any[] = []; let log: any[] = [];
beforeEach(() => { beforeEach(() => {
@ -693,6 +693,7 @@ describe('Integration', () => {
{provide: 'resolveFour', useValue: (a: any, b: any) => 4}, {provide: 'resolveFour', useValue: (a: any, b: any) => 4},
{provide: 'resolveSix', useClass: ResolveSix}, {provide: 'resolveSix', useClass: ResolveSix},
{provide: 'resolveError', useValue: (a: any, b: any) => Promise.reject('error')}, {provide: 'resolveError', useValue: (a: any, b: any) => Promise.reject('error')},
{provide: 'numberOfUrlSegments', useValue: (a: any, b: any) => a.url.length}
] ]
}); });
}); });
@ -787,6 +788,29 @@ describe('Integration', () => {
const cmp = fixture.debugElement.children[1].componentInstance; const cmp = fixture.debugElement.children[1].componentInstance;
expect(cmp.route.snapshot.data).toEqual({two: 2}); expect(cmp.route.snapshot.data).toEqual({two: 2});
}))); })));
it('should rerun resolvers when the urls segments of a wildcard route change',
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
const fixture = createRoot(router, RootCmp);
router.resetConfig([{
path: '**',
component: CollectParamsCmp,
resolve: {numberOfUrlSegments: 'numberOfUrlSegments'}
}]);
let e: any = null;
router.navigateByUrl('/one/two');
advance(fixture);
const cmp = fixture.debugElement.children[1].componentInstance;
expect(cmp.route.snapshot.data).toEqual({numberOfUrlSegments: 2});
router.navigateByUrl('/one/two/three');
advance(fixture);
expect(cmp.route.snapshot.data).toEqual({numberOfUrlSegments: 3});
})));
}); });
describe('router links', () => { describe('router links', () => {

View File

@ -95,6 +95,6 @@ function checkResolveData(
function createActivatedRouteSnapshot(cmp: string, extra: any = {}): ActivatedRouteSnapshot { function createActivatedRouteSnapshot(cmp: string, extra: any = {}): ActivatedRouteSnapshot {
return new ActivatedRouteSnapshot( return new ActivatedRouteSnapshot(
<any>null, {}, <any>null, <any>null, <any>null, <any>null, <any>cmp, <any>null, <any>null, -1, <any>[], {}, <any>null, <any>null, <any>null, <any>null, <any>cmp, <any>null, <any>null, -1,
extra.resolve); extra.resolve);
} }

View File

@ -6,7 +6,9 @@
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
import {ActivatedRoute, ActivatedRouteSnapshot, RouterState, RouterStateSnapshot} from '../src/router_state'; import {ActivatedRoute, ActivatedRouteSnapshot, RouterState, RouterStateSnapshot, equalParamsAndUrlSegments} from '../src/router_state';
import {Params} from '../src/shared';
import {UrlSegment} from '../src/url_tree';
import {TreeNode} from '../src/utils/tree'; import {TreeNode} from '../src/utils/tree';
describe('RouterState & Snapshot', () => { describe('RouterState & Snapshot', () => {
@ -93,6 +95,33 @@ describe('RouterState & Snapshot', () => {
expect(p[1]).toBe(b); expect(p[1]).toBe(b);
}); });
}); });
describe('equalParamsAndUrlSegments', () => {
function createSnapshot(params: Params, url: UrlSegment[]): ActivatedRouteSnapshot {
return new ActivatedRouteSnapshot(
url, params, <any>null, <any>null, <any>null, <any>null, <any>null, <any>null, <any>null,
-1, null);
}
it('should return false when params are different', () => {
expect(equalParamsAndUrlSegments(createSnapshot({a: 1}, []), createSnapshot({a: 2}, [])))
.toEqual(false);
});
it('should return false when urls are different', () => {
expect(equalParamsAndUrlSegments(
createSnapshot({a: 1}, [new UrlSegment('a', {})]),
createSnapshot({a: 1}, [new UrlSegment('b', {})])))
.toEqual(false);
});
it('should return true othewise', () => {
expect(equalParamsAndUrlSegments(
createSnapshot({a: 1}, [new UrlSegment('a', {})]),
createSnapshot({a: 1}, [new UrlSegment('a', {})])))
.toEqual(true);
});
});
}); });
function createActivatedRouteSnapshot(cmp: string) { function createActivatedRouteSnapshot(cmp: string) {