Unverified Commit f33b8af4 authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

Revert "Handle null history.state in client-side router popstate handler" (#90268)

Reverts vercel/next.js#90083

This causes an MPA navigation when updating the hash fragment via
`window.location.hash = "foo"`
parent 9404b901
...@@ -375,11 +375,7 @@ function Router({ ...@@ -375,11 +375,7 @@ function Router({
*/ */
const onPopState = (event: PopStateEvent) => { const onPopState = (event: PopStateEvent) => {
if (!event.state) { if (!event.state) {
// This case happens when pushState/replaceState was called outside // TODO-APP: this case only happens when pushState/replaceState was called outside of Next.js. It should probably reload the page in this case.
// of Next.js with a null state, or the initial history entry has no
// state. The router tree would be completely out of sync so we need
// to do a hard navigation to get back to a consistent state.
window.location.reload()
return return
} }
......
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
'use client'
import { usePathname } from 'next/navigation'
export default function Page() {
const pathname = usePathname()
return <p id="pathname">{pathname}</p>
}
'use client'
import Link from 'next/link'
import { usePathname } from 'next/navigation'
export default function Page() {
const pathname = usePathname()
return (
<>
<p id="pathname">{pathname}</p>
<Link href="/other" id="to-other">
Go to other
</Link>
<a href="#hash" id="hash-link">
Hash link
</a>
<div id="hash" style={{ marginTop: 1000 }}>
Hash target
</div>
</>
)
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {}
module.exports = nextConfig
import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'
describe('popstate-null-state', () => {
const { next } = nextTestSetup({
files: __dirname,
})
it('should hard navigate when history.state is null on popstate', async () => {
const browser = await next.browser('/')
// Verify the initial page is rendered
await retry(async () => {
expect(await browser.elementByCss('#pathname').text()).toBe('/')
})
// Navigate to /other via client-side navigation so the router tree updates
await browser.elementByCss('#to-other').click()
await retry(async () => {
expect(await browser.elementByCss('#pathname').text()).toBe('/other')
})
// Push a history entry with null state for '/', bypassing the Next.js
// patched pushState (which copies internal state). The patch is on the
// instance, so calling via the prototype bypasses it.
// History stack: [('/', nextjs), ('/other', nextjs), ('/', null), ('/', null)]
await browser.eval(
`History.prototype.pushState.call(window.history, null, '', '/')`
)
await browser.eval(
`History.prototype.pushState.call(window.history, null, '', '/')`
)
// Validate the push worked: URL changed and history.state is null
expect(await browser.eval('window.location.pathname')).toBe('/')
expect(await browser.eval('window.history.state')).toBe(null)
// Now go back — this triggers popstate with event.state === null
// (the state from the ('/', null) entry).
// Without the fix the router would do nothing, leaving pathname as '/other'.
// With the fix a hard navigation (reload) occurs, loading '/' fresh.
await browser.back()
await retry(async () => {
expect(await browser.elementByCss('#pathname').text()).toBe('/')
})
})
it('should handle back navigation to a hash change without full reload', async () => {
const browser = await next.browser('/')
// Verify the initial page is rendered
await retry(async () => {
expect(await browser.elementByCss('#pathname').text()).toBe('/')
})
// Click a regular anchor link. The browser pushes a new history entry
// with null state for the hash URL.
// History stack: [('/', nextjs), ('/#hash', null)]
await browser.elementByCss('#hash-link').click()
await retry(async () => {
expect(await browser.url()).toContain('#hash')
})
// Navigate to /other via Link so there's a forward entry.
// History stack: [('/', nextjs), ('/#hash', null), ('/other', nextjs)]
await browser.elementByCss('#to-other').click()
await retry(async () => {
expect(await browser.elementByCss('#pathname').text()).toBe('/other')
})
// Go back — popstate fires with the hash entry's state (null).
// Since only the hash changed, the page should remain functional
// without losing client-side state.
await browser.back()
await retry(async () => {
expect(await browser.url()).toContain('#hash')
expect(await browser.elementByCss('#pathname').text()).toBe('/')
})
})
})
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