Skip to content

Commit eb2ceff

Browse files
vsavkinchuckjaz
authored andcommitted
fix(router): should reset location if a navigation by location is successful (#13545)
Closes #13491
1 parent f49ab56 commit eb2ceff

2 files changed

Lines changed: 65 additions & 20 deletions

File tree

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

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -266,14 +266,16 @@ function defaultErrorHandler(error: any): any {
266266
throw error;
267267
}
268268

269+
type NavigationSource = 'imperative' | 'popstate' | 'hashchange';
270+
269271
type NavigationParams = {
270272
id: number,
271273
rawUrl: UrlTree,
272274
extras: NavigationExtras,
273275
resolve: any,
274276
reject: any,
275277
promise: Promise<boolean>,
276-
imperative: boolean,
278+
source: NavigationSource,
277279
};
278280

279281

@@ -374,20 +376,8 @@ export class Router {
374376
if (!this.locationSubscription) {
375377
this.locationSubscription = <any>this.location.subscribe(Zone.current.wrap((change: any) => {
376378
const rawUrlTree = this.urlSerializer.parse(change['url']);
377-
const lastNavigation = this.navigations.value;
378-
379-
// If the user triggers a navigation imperatively (e.g., by using navigateByUrl),
380-
// and that navigation results in 'replaceState' that leads to the same URL,
381-
// we should skip those.
382-
if (lastNavigation && lastNavigation.imperative &&
383-
lastNavigation.rawUrl.toString() === rawUrlTree.toString()) {
384-
return;
385-
}
386-
387-
setTimeout(() => {
388-
this.scheduleNavigation(
389-
rawUrlTree, false, {skipLocationChange: change['pop'], replaceUrl: true});
390-
}, 0);
379+
const source: NavigationSource = change['type'] === 'popstate' ? 'popstate' : 'hashchange';
380+
setTimeout(() => { this.scheduleNavigation(rawUrlTree, source, {replaceUrl: true}); }, 0);
391381
}));
392382
}
393383
}
@@ -505,12 +495,12 @@ export class Router {
505495
Promise<boolean> {
506496
if (url instanceof UrlTree) {
507497
return this.scheduleNavigation(
508-
this.urlHandlingStrategy.merge(url, this.rawUrlTree), true, extras);
498+
this.urlHandlingStrategy.merge(url, this.rawUrlTree), 'imperative', extras);
509499
}
510500

511501
const urlTree = this.urlSerializer.parse(url);
512502
return this.scheduleNavigation(
513-
this.urlHandlingStrategy.merge(urlTree, this.rawUrlTree), true, extras);
503+
this.urlHandlingStrategy.merge(urlTree, this.rawUrlTree), 'imperative', extras);
514504
}
515505

516506
/**
@@ -585,8 +575,26 @@ export class Router {
585575
.subscribe(() => {});
586576
}
587577

588-
private scheduleNavigation(rawUrl: UrlTree, imperative: boolean, extras: NavigationExtras):
578+
private scheduleNavigation(rawUrl: UrlTree, source: NavigationSource, extras: NavigationExtras):
589579
Promise<boolean> {
580+
const lastNavigation = this.navigations.value;
581+
582+
// If the user triggers a navigation imperatively (e.g., by using navigateByUrl),
583+
// and that navigation results in 'replaceState' that leads to the same URL,
584+
// we should skip those.
585+
if (lastNavigation && source !== 'imperative' && lastNavigation.source === 'imperative' &&
586+
lastNavigation.rawUrl.toString() === rawUrl.toString()) {
587+
return null; // return value is not used
588+
}
589+
590+
// Because of a bug in IE and Edge, the location class fires two events (popstate and
591+
// hashchange)
592+
// every single time. The second one should be ignored. Otherwise, the URL will flicker.
593+
if (lastNavigation && source == 'hashchange' && lastNavigation.source === 'popstate' &&
594+
lastNavigation.rawUrl.toString() === rawUrl.toString()) {
595+
return null; // return value is not used
596+
}
597+
590598
let resolve: any = null;
591599
let reject: any = null;
592600

@@ -596,7 +604,7 @@ export class Router {
596604
});
597605

598606
const id = ++this.navigationId;
599-
this.navigations.next({id, imperative, rawUrl, extras, resolve, reject, promise});
607+
this.navigations.next({id, source, rawUrl, extras, resolve, reject, promise});
600608

601609
// Make sure that the error is propagated even though `processNavigations` catch
602610
// handler does not rethrow

‎modules/@angular/router/test/integration.spec.ts‎

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,43 @@ describe('Integration', () => {
389389
expect(cmp.recordedUrls()).toEqual(['one/two', 'three/four']);
390390
})));
391391

392+
describe('should reset location if a navigation by location is successful', () => {
393+
beforeEach(() => {
394+
TestBed.configureTestingModule({
395+
providers: [{
396+
provide: 'in1Second',
397+
useValue: (c: any, a: ActivatedRouteSnapshot, b: RouterStateSnapshot) => {
398+
let res: any = null;
399+
const p = new Promise(_ => res = _);
400+
setTimeout(() => res(true), 1000);
401+
return p;
402+
}
403+
}]
404+
});
405+
});
406+
407+
it('work', fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
408+
const fixture = createRoot(router, RootCmp);
409+
410+
router.resetConfig([{path: 'simple', component: SimpleCmp, canActivate: ['in1Second']}]);
411+
412+
// Trigger two location changes to the same URL.
413+
// Because of the guard the order will look as follows:
414+
// - location change 'simple'
415+
// - start processing the change, start a guard
416+
// - location change 'simple'
417+
// - the first location change gets canceled, the URL gets reset to '/'
418+
// - the second location change gets finished, the URL should be reset to '/simple'
419+
(<any>location).simulateUrlPop('/simple');
420+
(<any>location).simulateUrlPop('/simple');
421+
422+
tick(2000);
423+
advance(fixture);
424+
425+
expect(location.path()).toEqual('/simple');
426+
})));
427+
});
428+
392429
it('should support secondary routes', fakeAsync(inject([Router], (router: Router) => {
393430
const fixture = createRoot(router, RootCmp);
394431

@@ -1416,7 +1453,7 @@ describe('Integration', () => {
14161453
log.push('called');
14171454
return false;
14181455
}
1419-
},
1456+
}
14201457
]
14211458
});
14221459
});

0 commit comments

Comments
 (0)