Unverified Commit 7c119824 authored by Dan Abramov's avatar Dan Abramov Committed by GitHub

Skip the initial history write on any activation-key mismatch

When a history entry the router does not own is current at the first
commit, the write-site check correctly declined to treat it as a
restorable traversal — but then fell through to the normal history
write, stamping __NA and the rendered tree onto the foreign entry. The
replay check then saw a restorable entry and dispatched a spurious
restore. And if a further history write landed between the store being
created and that first commit, the write also reverted the entry's URL,
since canonicalUrl is read from location.href when the store is
created.

Skip the write whenever the current entry is not the activation entry:
the render does not describe that entry, so writing to it is wrong
regardless of ownership. Restorability (__NA) still gates only the
replay.

This turns the assertions from the parent commit green. The __NA stamp
is the deterministic half of the damage; reverting the URL needs
another write inside the hydration window, which is why this showed up
as flakiness rather than a failing test.

It was the dominant cause of the back-before-hydration suite's dev
flakiness on CI: an instrumented run showed the false-positive replays
and foreign-entry writes directly, with all five duplicated suite
copies failing beforehand and all five passing on first attempt with
this change (runs 31614244097 and 31618763624 on #97245).
Co-Authored-By: 's avatarClaude Fable 5 <noreply@anthropic.com>
parent 71c3c22a
...@@ -56,13 +56,10 @@ const globalMutable: { ...@@ -56,13 +56,10 @@ const globalMutable: {
pendingMpaPath?: string pendingMpaPath?: string
} = {} } = {}
// A Back/Forward press before the router's popstate listener exists moves the // The current entry is not the one this document was activated on. Entry keys
// browser to a different history entry than the one the document was activated // are stable across replaceState, so until the popstate listener is installed
// on, and the resulting popstate fires with nobody listening. The activation // this means a traversal or history write went unobserved.
// entry is fixed for the document's lifetime and entry keys are stable across function isOnUnobservedEntry(): boolean {
// replaceState, so until the listener is installed a key mismatch means a
// traversal went unobserved.
function hasMissedTraversal(): boolean {
if (typeof window.navigation === 'undefined') { if (typeof window.navigation === 'undefined') {
return false return false
} }
...@@ -71,10 +68,7 @@ function hasMissedTraversal(): boolean { ...@@ -71,10 +68,7 @@ function hasMissedTraversal(): boolean {
return ( return (
activationEntry != null && activationEntry != null &&
currentEntry != null && currentEntry != null &&
activationEntry.key !== currentEntry.key && activationEntry.key !== currentEntry.key
// Only entries written by the app router can be restored; on any other
// entry the traversal is left unhandled, as before.
window.history.state?.__NA === true
) )
} }
...@@ -125,8 +119,8 @@ function HistoryUpdater({ ...@@ -125,8 +119,8 @@ function HistoryUpdater({
if (!checkedMissedTraversalBeforeHistoryWrite) { if (!checkedMissedTraversalBeforeHistoryWrite) {
checkedMissedTraversalBeforeHistoryWrite = true checkedMissedTraversalBeforeHistoryWrite = true
if (hasMissedTraversal()) { if (isOnUnobservedEntry()) {
// Skip the write: it would overwrite the traversed-to entry's state. // This render does not describe the current entry; don't write to it.
// The tree was rendered even though the history write is skipped. // The tree was rendered even though the history write is skipped.
setLastCommittedTree(tree) setLastCommittedTree(tree)
return return
...@@ -440,7 +434,12 @@ function Router({ ...@@ -440,7 +434,12 @@ function Router({
if (!checkedMissedTraversalBeforeReplay) { if (!checkedMissedTraversalBeforeReplay) {
checkedMissedTraversalBeforeReplay = true checkedMissedTraversalBeforeReplay = true
if (hasMissedTraversal()) { if (
isOnUnobservedEntry() &&
// Only entries written by the app router can be restored; on any
// other entry the traversal is left unhandled.
window.history.state?.__NA === true
) {
handlePopState(window.history.state) handlePopState(window.history.state)
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment