Unverified Commit 71c3c22a authored by Dan Abramov's avatar Dan Abramov Committed by GitHub

Assert that the router leaves foreign history entries alone

If the entry that is current at the first commit is not the one the
document was activated on, and the router did not write it — a native
anchor entry, or a third-party push that landed before the router's
wrappers were installed — then the render does not describe that entry
and the router has no business writing to it.

Every test that ends up on such an entry now checks its history state,
and a new one checks its URL: when the push lands after the router's
store captured the URL, writing to the entry puts that URL back and
undoes the push.

They fail. The write puts __NA and the rendered tree on the entry and
resets its URL, and the __NA in turn makes the replay check treat the
entry as restorable.
Co-Authored-By: 's avatarClaude Opus 5 <noreply@anthropic.com>
parent 2fbeebba
......@@ -2,18 +2,33 @@
import { useEffect } from 'react'
// Simulates a third-party script writing to history between the router's
// traversal detection and its popstate replay: this component's effect runs
// after all insertion effects but before the parent router's effects.
// Simulates a third-party script writing to history at one of two points the
// router cares about. The test picks one by setting `window.__thirdPartyPush`:
//
// 'render' — during hydration, after the router's store captured the URL but
// before its first history write. A real script gets here from a timer or a
// deferred script that fires while the page is hydrating.
// 'effect' — after the router detected the missed traversal but before it
// replays it. Child effects run before the parent router's.
function pushIfArmed(at: 'render' | 'effect') {
if (
typeof window === 'undefined' ||
(window as any).__thirdPartyPush !== at
) {
return
}
;(window as any).__thirdPartyPush = undefined
window.history.pushState(
{ thirdParty: true },
'',
window.location.pathname + '?tp=1'
)
}
export function ThirdPartyPush() {
pushIfArmed('render')
useEffect(() => {
if ((window as any).__injectThirdPartyPush) {
window.history.pushState(
{ thirdParty: true },
'',
window.location.pathname + '?tp=1'
)
}
pushIfArmed('effect')
}, [])
return null
}
......@@ -191,11 +191,42 @@ describe('back navigation before hydration after reload', () => {
expect(new URL(await browser.url()).search).toBe('?tp=1')
})
// The router must not claim an entry it did not write.
expect(await browser.eval('window.history.state')).toEqual({
thirdParty: true,
})
// The router still navigates.
await browser.elementById('to-home').click()
await waitForPage(browser, '#home')
})
it('keeps a pushState that lands during hydration', async () => {
const { browser, page, releaseScripts } = await loadStalled(
postPath,
'#post'
)
// Unlike the test above, this one pushes after the router's store has
// captured the URL. Writing to the entry would put that captured URL
// back and undo the push.
await page.evaluate("window.__thirdPartyPush = 'render'")
releaseScripts()
await retry(async () => {
expect(await browser.eval('window.__stayed')).toBe(true)
expect(await browser.elementByCss('h1').text()).toBe('Post')
expect(new URL(await browser.url()).search).toBe('?tp=1')
})
expect(await browser.eval('window.history.state')).toEqual({
thirdParty: true,
})
await browser.elementById('to-home').click()
await waitForPage(browser, '#home')
})
it('keeps an in-page anchor jump on a fresh load', async () => {
const { browser, page, releaseScripts } = await loadStalled(
postPath,
......@@ -212,6 +243,9 @@ describe('back navigation before hydration after reload', () => {
expect(new URL(await browser.url()).hash).toBe('#section')
})
// The browser created this entry, not the router.
expect(await browser.eval('window.history.state')).toBe(null)
// Hash traversals keep behaving like same-page jumps.
await browser.back()
await retry(async () => {
......@@ -242,6 +276,8 @@ describe('back navigation before hydration after reload', () => {
expect(new URL(await browser.url()).hash).toBe('#section')
})
expect(await browser.eval('window.history.state')).toBe(null)
// Traversing over the hash entry and the pushState entry still works.
await browser.back() // -> post
await browser.back() // -> home
......@@ -285,7 +321,7 @@ describe('back navigation before hydration after reload', () => {
// Arms an effect in the fixture that pushes a third-party history
// entry between the router's traversal detection and its replay.
await page.evaluate('window.__injectThirdPartyPush = true')
await page.evaluate("window.__thirdPartyPush = 'effect'")
releaseScripts()
await retry(async () => {
......@@ -323,6 +359,8 @@ describe('back navigation before hydration after reload', () => {
expect(await browser.eval('window.__stayed')).toBe(true)
expect(await browser.elementByCss('h1').text()).toBe('Post')
})
expect(await browser.eval('window.history.state')).toEqual({ a: 1 })
})
})
})
......
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