@@ -266,14 +266,16 @@ function defaultErrorHandler(error: any): any {
266266 throw error ;
267267}
268268
269+ type NavigationSource = 'imperative' | 'popstate' | 'hashchange' ;
270+
269271type 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
0 commit comments