@@ -42,6 +42,7 @@ function countLeadingRevealPadding(history: CustomHistoryEntry[] | undefined): n
4242 return 0 ;
4343 }
4444 let count = 0 ;
45+ // Count how many fake history slots we previously added to keep browser history aligned.
4546 for ( const entry of history ) {
4647 if ( entry === CONST . NAVIGATION . CUSTOM_HISTORY_ENTRY_REVEAL_PADDING ) {
4748 count += 1 ;
@@ -73,6 +74,7 @@ function getFrozenHistoryStateForReplaceFullscreenUnderRHP(
7374 return { pendingReveal, state : rehydrate ( newState , configOptions ) } ;
7475 }
7576
77+ // Capture the RHP that should be dismissed in the second half of the reveal.
7678 const topRoute = state . routes . at ( - 1 ) ;
7779 const postReplaceRoutesLength = ( newState . routes ?? state . routes ) . length ;
7880 const nextPendingReveal =
@@ -84,6 +86,7 @@ function getFrozenHistoryStateForReplaceFullscreenUnderRHP(
8486 }
8587 : pendingReveal ;
8688
89+ // Use the new routes but freeze old history for this hidden intermediate frame.
8790 // Defensive copy of state.history (shared reference; freeze must not alias).
8891 return { pendingReveal : nextPendingReveal , state : { ...rehydrate ( newState , configOptions ) , history : [ ...state . history ] } } ;
8992}
@@ -99,6 +102,7 @@ function getFrozenHistoryStateForRemoveFullscreenUnderRHP(
99102 return rehydrate ( newState , configOptions ) ;
100103 }
101104
105+ // Cancel path: remove the pre-inserted screen while keeping browser history still.
102106 return { ...rehydrate ( newState , configOptions ) , history : [ ...state . history ] } ;
103107}
104108
@@ -109,11 +113,12 @@ function getRevealDismissState(
109113 pendingReveal : PendingReveal ,
110114 rehydrate : RehydrateRootHistoryState ,
111115) : { pendingReveal : PendingReveal | null ; state ?: RootHistoryState } {
112- // `state` is the pre-DISMISS state; `state.routes.at(-1)` is what will be popped .
116+ // This is the stack before closing the RHP; the top route is the one DISMISS removes .
113117 const dismissingTopKey = state . routes . at ( - 1 ) ?. key ;
114118 const depthMatches = state . routes . length === pendingReveal . routesLengthAtCapture ;
115119 const historyDepthMatches = state . history ?. length === pendingReveal . historyLengthAtCapture ;
116120
121+ // Apply the reveal fix only when this DISMISS closes the same RHP we snapshotted.
117122 if ( dismissingTopKey === pendingReveal . rhpKey && depthMatches && historyDepthMatches ) {
118123 const rehydrated = rehydrate ( newState , configOptions ) ;
119124 const rehydratedHistory = asCustomHistory ( rehydrated . history ) ?? [ ] ;
@@ -122,14 +127,15 @@ function getRevealDismissState(
122127 const lengthDelta = ( state . history ?. length ?? 0 ) - rehydratedHistory . length ;
123128 if ( lengthDelta > 0 ) {
124129 Log . hmmm ( `[addRootHistoryRouterExtension] reveal committed; freezing history with offset ${ lengthDelta } ` ) ;
130+ // Keep the pre-dismiss history length so web linking replaces instead of going back.
125131 return { pendingReveal : null , state : { ...rehydrated , history : buildPaddedHistory ( rehydratedHistory , lengthDelta ) } } ;
126132 }
127133 Log . hmmm ( '[addRootHistoryRouterExtension] reveal committed with non-positive lengthDelta; no freeze' , { lengthDelta} ) ;
128134 return { pendingReveal : null } ;
129135 }
130136
131137 if ( dismissingTopKey === pendingReveal . rhpKey ) {
132- // Key match but depth divergence: drop snapshot, let DISMISS proceed naturally .
138+ // Same RHP, but the stack/history changed unexpectedly; skip the special reveal fix .
133139 Log . hmmm ( '[addRootHistoryRouterExtension] reveal snapshot key matched but depth diverged; clearing without freeze' , {
134140 capturedRoutesLength : pendingReveal . routesLengthAtCapture ,
135141 currentRoutesLength : state . routes . length ,
@@ -143,6 +149,7 @@ function getRevealDismissState(
143149}
144150
145151function applyRevealPaddingOffset ( state : RootHistoryState , rehydrated : RootHistoryState ) : RootHistoryState {
152+ // Regular navigation rebuilds history from routes; put back any fake slots already in use.
146153 const offset = countLeadingRevealPadding ( asCustomHistory ( state . history ) ) ;
147154 if ( offset > 0 ) {
148155 return { ...rehydrated , history : buildPaddedHistory ( asCustomHistory ( rehydrated . history ) ?? [ ] , offset ) } ;
0 commit comments