@@ -322,7 +322,7 @@ export class Router {
322322 this . navigationTransitions . setupNavigations ( this ) . subscribe (
323323 t => {
324324 this . lastSuccessfulId = t . id ;
325- this . currentPageId = t . targetPageId ;
325+ this . currentPageId = this . browserPageId ?? 0 ;
326326 } ,
327327 e => {
328328 this . console . warn ( `Unhandled Navigation Error: ${ e } ` ) ;
@@ -699,13 +699,9 @@ export class Router {
699699 if ( restoredState && restoredState . ɵrouterPageId ) {
700700 targetPageId = restoredState . ɵrouterPageId ;
701701 } else {
702- // If we're replacing the URL or doing a silent navigation, we do not want to increment the
703- // page id because we aren't pushing a new entry to history.
704- if ( extras . replaceUrl || extras . skipLocationChange ) {
705- targetPageId = this . browserPageId ?? 0 ;
706- } else {
707- targetPageId = ( this . browserPageId ?? 0 ) + 1 ;
708- }
702+ // Otherwise, targetPageId should be the next number in the event of a `pushState`
703+ // navigation.
704+ targetPageId = ( this . browserPageId ?? 0 ) + 1 ;
709705 }
710706 } else {
711707 // This is unused when `canceledNavigationResolution` is not computed.
@@ -737,13 +733,20 @@ export class Router {
737733 /** @internal */
738734 setBrowserUrl ( url : UrlTree , transition : NavigationTransition ) {
739735 const path = this . urlSerializer . serialize ( url ) ;
740- const state = {
741- ...transition . extras . state ,
742- ...this . generateNgRouterState ( transition . id , transition . targetPageId )
743- } ;
744736 if ( this . location . isCurrentPathEqualTo ( path ) || ! ! transition . extras . replaceUrl ) {
737+ // replacements do not update the target page
738+ const currentBrowserPageId =
739+ this . canceledNavigationResolution === 'computed' ? this . browserPageId : undefined ;
740+ const state = {
741+ ...transition . extras . state ,
742+ ...this . generateNgRouterState ( transition . id , currentBrowserPageId )
743+ } ;
745744 this . location . replaceState ( path , '' , state ) ;
746745 } else {
746+ const state = {
747+ ...transition . extras . state ,
748+ ...this . generateNgRouterState ( transition . id , transition . targetPageId )
749+ } ;
747750 this . location . go ( path , '' , state ) ;
748751 }
749752 }
@@ -755,16 +758,9 @@ export class Router {
755758 */
756759 restoreHistory ( transition : NavigationTransition , restoringFromCaughtError = false ) {
757760 if ( this . canceledNavigationResolution === 'computed' ) {
758- const targetPagePosition = this . currentPageId - transition . targetPageId ;
759- // The navigator change the location before triggered the browser event,
760- // so we need to go back to the current url if the navigation is canceled.
761- // Also, when navigation gets cancelled while using url update strategy eager, then we need to
762- // go back. Because, when `urlUpdateStrategy` is `eager`; `setBrowserUrl` method is called
763- // before any verification.
764- const browserUrlUpdateOccurred =
765- ( transition . source === 'popstate' || this . urlUpdateStrategy === 'eager' ||
766- this . currentUrlTree === this . getCurrentNavigation ( ) ?. finalUrl ) ;
767- if ( browserUrlUpdateOccurred && targetPagePosition !== 0 ) {
761+ const currentBrowserPageId = this . browserPageId ?? this . currentPageId ;
762+ const targetPagePosition = this . currentPageId - currentBrowserPageId ;
763+ if ( targetPagePosition !== 0 ) {
768764 this . location . historyGo ( targetPagePosition ) ;
769765 } else if (
770766 this . currentUrlTree === this . getCurrentNavigation ( ) ?. finalUrl &&
0 commit comments