Skip to content

Commit b2a7fd0

Browse files
dloukadakismhevery
authored andcommitted
fix(router): replace state when path is equal to current path (angular#8766)
Same as 2bf21e1 but for new router. This also fixes an issue where when application loads it clears forward history because Router constructor calls navigateByUrl which was causing a push state to happen.
1 parent 84f859d commit b2a7fd0

2 files changed

Lines changed: 26 additions & 3 deletions

File tree

modules/@angular/router/src/router.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ export class Router {
151151
(change) => { this._navigate(this._urlSerializer.parse(change['url']), change['pop']); });
152152
}
153153

154-
private _navigate(url: UrlTree, pop?: boolean): Promise<void> {
154+
private _navigate(url: UrlTree, preventPushState?: boolean): Promise<void> {
155155
this._urlTree = url;
156156
return recognize(this._componentResolver, this._rootComponentType, url, this._routeTree)
157157
.then(currTree => {
@@ -160,8 +160,13 @@ export class Router {
160160
.then(updated => {
161161
if (updated) {
162162
this._routeTree = currTree;
163-
if (isBlank(pop) || !pop) {
164-
this._location.go(this._urlSerializer.serialize(this._urlTree));
163+
if (isBlank(preventPushState) || !preventPushState) {
164+
let path = this._urlSerializer.serialize(this._urlTree);
165+
if (this._location.isCurrentPathEqualTo(path)) {
166+
this._location.replaceState(path);
167+
} else {
168+
this._location.go(path);
169+
}
165170
}
166171
this._changes.emit(null);
167172
}

modules/@angular/router/test/integration_spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,24 @@ export function main() {
252252
advance(fixture);
253253
expect(fixture.debugElement.nativeElement).toHaveText('link');
254254
})));
255+
256+
it('should replace state when path is equal to current path',
257+
fakeAsync(inject([Router, TestComponentBuilder, Location], (router, tcb, location) => {
258+
let fixture = tcb.createFakeAsync(RootCmp);
259+
260+
router.navigateByUrl('/team/33/simple');
261+
advance(fixture);
262+
263+
router.navigateByUrl('/team/22/user/victor');
264+
advance(fixture);
265+
266+
router.navigateByUrl('/team/22/user/victor');
267+
advance(fixture);
268+
269+
location.back();
270+
advance(fixture);
271+
expect(location.path()).toEqual('/team/33/simple');
272+
})));
255273
}
256274
});
257275
}

0 commit comments

Comments
 (0)