@@ -329,7 +329,7 @@ export class Router {
329329 this . navigationTransitions . setupNavigations ( this ) . subscribe (
330330 t => {
331331 this . lastSuccessfulId = t . id ;
332- this . currentPageId = t . targetPageId ;
332+ this . currentPageId = this . browserPageId ?? 0 ;
333333 } ,
334334 e => {
335335 this . console . warn ( `Unhandled Navigation Error: ${ e } ` ) ;
@@ -733,13 +733,9 @@ export class Router {
733733 if ( restoredState && restoredState . ɵrouterPageId ) {
734734 targetPageId = restoredState . ɵrouterPageId ;
735735 } else {
736- // If we're replacing the URL or doing a silent navigation, we do not want to increment the
737- // page id because we aren't pushing a new entry to history.
738- if ( extras . replaceUrl || extras . skipLocationChange ) {
739- targetPageId = this . browserPageId ?? 0 ;
740- } else {
741- targetPageId = ( this . browserPageId ?? 0 ) + 1 ;
742- }
736+ // Otherwise, targetPageId should be the next number in the event of a `pushState`
737+ // navigation.
738+ targetPageId = ( this . browserPageId ?? 0 ) + 1 ;
743739 }
744740 } else {
745741 // This is unused when `canceledNavigationResolution` is not computed.
@@ -779,13 +775,20 @@ export class Router {
779775 /** @internal */
780776 setBrowserUrl ( url : UrlTree , transition : NavigationTransition ) {
781777 const path = this . urlSerializer . serialize ( url ) ;
782- const state = {
783- ...transition . extras . state ,
784- ...this . generateNgRouterState ( transition . id , transition . targetPageId )
785- } ;
786778 if ( this . location . isCurrentPathEqualTo ( path ) || ! ! transition . extras . replaceUrl ) {
779+ // replacements do not update the target page
780+ const currentBrowserPageId =
781+ this . canceledNavigationResolution === 'computed' ? this . browserPageId : undefined ;
782+ const state = {
783+ ...transition . extras . state ,
784+ ...this . generateNgRouterState ( transition . id , currentBrowserPageId )
785+ } ;
787786 this . location . replaceState ( path , '' , state ) ;
788787 } else {
788+ const state = {
789+ ...transition . extras . state ,
790+ ...this . generateNgRouterState ( transition . id , transition . targetPageId )
791+ } ;
789792 this . location . go ( path , '' , state ) ;
790793 }
791794 }
@@ -797,16 +800,9 @@ export class Router {
797800 */
798801 restoreHistory ( transition : NavigationTransition , restoringFromCaughtError = false ) {
799802 if ( this . canceledNavigationResolution === 'computed' ) {
800- const targetPagePosition = this . currentPageId - transition . targetPageId ;
801- // The navigator change the location before triggered the browser event,
802- // so we need to go back to the current url if the navigation is canceled.
803- // Also, when navigation gets cancelled while using url update strategy eager, then we need to
804- // go back. Because, when `urlUpdateStrategy` is `eager`; `setBrowserUrl` method is called
805- // before any verification.
806- const browserUrlUpdateOccurred =
807- ( transition . source === 'popstate' || this . urlUpdateStrategy === 'eager' ||
808- this . currentUrlTree === this . getCurrentNavigation ( ) ?. finalUrl ) ;
809- if ( browserUrlUpdateOccurred && targetPagePosition !== 0 ) {
803+ const currentBrowserPageId = this . browserPageId ?? this . currentPageId ;
804+ const targetPagePosition = this . currentPageId - currentBrowserPageId ;
805+ if ( targetPagePosition !== 0 ) {
810806 this . location . historyGo ( targetPagePosition ) ;
811807 } else if (
812808 this . currentUrlTree === this . getCurrentNavigation ( ) ?. finalUrl &&
0 commit comments