Skip to content

Commit

Permalink
fix(router): guards restor an incorrect url when used with skipLocati…
Browse files Browse the repository at this point in the history
…onChange

Closes #12825
  • Loading branch information
vsavkin authored and chuckjaz committed Nov 17, 2016
1 parent 602522b commit ad20d7d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
9 changes: 6 additions & 3 deletions modules/@angular/router/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ type NavigationParams = {
export class Router {
private currentUrlTree: UrlTree;
private rawUrlTree: UrlTree;
private currentUrlTreeStoredInLocation: UrlTree;

private navigations: BehaviorSubject<NavigationParams> =
new BehaviorSubject<NavigationParams>(null);
Expand Down Expand Up @@ -716,6 +717,7 @@ export class Router {
let navigationIsSuccessful: boolean;
const storedState = this.currentRouterState;
const storedUrl = this.currentUrlTree;
const storedUrlInLocation = this.currentUrlTreeStoredInLocation;

routerState$
.forEach(({appliedUrl, state, shouldActivate}: any) => {
Expand All @@ -726,6 +728,8 @@ export class Router {

this.currentUrlTree = appliedUrl;
this.rawUrlTree = this.urlHandlingStrategy.merge(this.currentUrlTree, rawUrl);
this.currentUrlTreeStoredInLocation =
shouldPreventPushState ? this.currentUrlTreeStoredInLocation : this.rawUrlTree;

this.currentRouterState = state;

Expand Down Expand Up @@ -774,14 +778,13 @@ export class Router {
this.currentRouterState = storedState;
this.currentUrlTree = storedUrl;
this.rawUrlTree = this.urlHandlingStrategy.merge(this.currentUrlTree, rawUrl);
this.location.replaceState(this.serializeUrl(this.rawUrlTree));
this.location.replaceState(this.serializeUrl(storedUrlInLocation));
});
});
}

private resetUrlToCurrentUrlTree(): void {
const path = this.urlSerializer.serialize(this.rawUrlTree);
this.location.replaceState(path);
this.location.replaceState(this.urlSerializer.serialize(this.currentUrlTreeStoredInLocation));
}
}

Expand Down
21 changes: 21 additions & 0 deletions modules/@angular/router/test/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1532,6 +1532,27 @@ describe('Integration', () => {
expect(location.path()).toEqual('/main/component1');
})));

it('should respect skipLocationChange when guards cancel navigations',
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
const fixture = createRoot(router, RootCmp);

router.resetConfig([
{path: 'component1', component: SimpleCmp},
{path: 'component2', component: SimpleCmp, canDeactivate: ['alwaysFalse']}
]);

router.navigateByUrl('/component1'); // initial state
advance(fixture);

router.navigateByUrl('/component2', {skipLocationChange: true});
advance(fixture);
expect(location.path()).toEqual('/component1');

location.back();
advance(fixture);

expect(location.path()).toEqual('/component1');
})));
});

describe('should work when given a class', () => {
Expand Down

0 comments on commit ad20d7d

Please sign in to comment.