Unverified Commit 3baea357 authored by dan's avatar dan Committed by GitHub

Revert "Replay same-document traversals that happen before hydration" (#95853)

Reverts vercel/next.js#95682

This breaks CC (https://github.com/vercel/next.js/issues/95848) due to
PPR codepath doing something different. Although Claude says
https://github.com/vercel/next.js/commit/da4d63d759a9bffd3e8c586bac1e4dabffd5b35d
can work around it, I think it's safest to revert this given that I
don't know what the right full fix is.

I also think we need more test coverage because the navigation test
suite is off in CC mode.

I'll send a follow-up with all the failing tests we know of (from #95682
and new) so we can fix this for good after a closer look.
parent 3ae5630d
...@@ -56,43 +56,6 @@ const globalMutable: { ...@@ -56,43 +56,6 @@ const globalMutable: {
pendingMpaPath?: string pendingMpaPath?: string
} = {} } = {}
// A Back/Forward press between a reload and hydration moves the browser to
// a different history entry than the one the document was activated on, and
// the resulting popstate fires before any listener exists. When that
// happened, the initial history write is skipped so the traversed entry's
// state is not overwritten, and the missed popstate is replayed once the
// router has mounted.
let missedTraversalBeforeHydration: 'unchecked' | 'missed' | 'none' =
'unchecked'
/**
* Handles a popstate event (or one that was missed before hydration).
* By default dispatches ACTION_RESTORE, however if the history entry was not
* pushed/replaced by app-router it will reload the page.
* That case can happen when the old router injected the history entry.
*/
function handlePopState(state: PopStateEvent['state']): void {
if (!state) {
// TODO-APP: this case only happens when pushState/replaceState was called outside of Next.js. It should probably reload the page in this case.
return
}
// This case happens when the history entry was pushed by the `pages` router.
if (!state.__NA) {
window.location.reload()
return
}
// TODO-APP: Ideally the back button should not use startTransition as it should apply the updates synchronously
// Without startTransition works if the cache is there for this path
startTransition(() => {
dispatchTraverseAction(
window.location.href,
state.__PRIVATE_NEXTJS_INTERNALS_TREE
)
})
}
function HistoryUpdater({ function HistoryUpdater({
appRouterState, appRouterState,
}: { }: {
...@@ -107,28 +70,6 @@ function HistoryUpdater({ ...@@ -107,28 +70,6 @@ function HistoryUpdater({
const { tree, pushRef, canonicalUrl, renderedSearch } = appRouterState const { tree, pushRef, canonicalUrl, renderedSearch } = appRouterState
if (missedTraversalBeforeHydration === 'unchecked') {
missedTraversalBeforeHydration = 'none'
if (typeof window.navigation !== 'undefined') {
// Entry keys are stable across replaceState but not traversals.
const activationEntry = window.navigation.activation?.entry
const currentEntry = window.navigation.currentEntry
if (
activationEntry != null &&
currentEntry != null &&
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
) {
missedTraversalBeforeHydration = 'missed'
// The tree was rendered even though the history write is skipped.
setLastCommittedTree(tree)
return
}
}
}
const appHistoryState: AppHistoryState = { const appHistoryState: AppHistoryState = {
tree, tree,
renderedSearch, renderedSearch,
...@@ -430,21 +371,35 @@ function Router({ ...@@ -430,21 +371,35 @@ function Router({
return originalReplaceState(data, _unused, url) return originalReplaceState(data, _unused, url)
} }
const onPopState = (event: PopStateEvent) => handlePopState(event.state) /**
* Handle popstate event, this is used to handle back/forward in the browser.
// Register popstate event to call onPopstate. * By default dispatches ACTION_RESTORE, however if the history entry was not pushed/replaced by app-router it will reload the page.
window.addEventListener('popstate', onPopState) * That case can happen when the old router injected the history entry.
*/
const onPopState = (event: PopStateEvent) => {
if (!event.state) {
// TODO-APP: this case only happens when pushState/replaceState was called outside of Next.js. It should probably reload the page in this case.
return
}
if (missedTraversalBeforeHydration === 'missed') { // This case happens when the history entry was pushed by the `pages` router.
missedTraversalBeforeHydration = 'none' if (!event.state.__NA) {
// The entry's state may have been rewritten by a third-party script window.location.reload()
// since the traversal was detected. return
const entryHistoryState = window.history.state
if (entryHistoryState?.__NA) {
handlePopState(entryHistoryState)
} }
// TODO-APP: Ideally the back button should not use startTransition as it should apply the updates synchronously
// Without startTransition works if the cache is there for this path
startTransition(() => {
dispatchTraverseAction(
window.location.href,
event.state.__PRIVATE_NEXTJS_INTERNALS_TREE
)
})
} }
// Register popstate event to call onPopstate.
window.addEventListener('popstate', onPopState)
return () => { return () => {
window.history.pushState = originalPushState window.history.pushState = originalPushState
window.history.replaceState = originalReplaceState window.history.replaceState = originalReplaceState
......
import { ThirdPartyPush } from './third-party-push'
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body>
<ThirdPartyPush />
{children}
</body>
</html>
)
}
import Link from 'next/link'
export default function Home() {
return (
<>
<h1 id="home">Home</h1>
<Link id="to-post" href="/post">
To post
</Link>
</>
)
}
import Link from 'next/link'
export default function Post() {
return (
<>
<h1 id="post">Post</h1>
<Link id="to-home" href="/">
To home
</Link>
<a id="hash-link" href="#section">
Jump to section
</a>
<div id="section">Section</div>
</>
)
}
import Link from 'next/link'
import { Suspense } from 'react'
async function CurrentPage({
searchParams,
}: {
searchParams: Promise<{ page?: string }>
}) {
const { page = '1' } = await searchParams
return <h1 id="search">Page {page}</h1>
}
export default function Search({
searchParams,
}: {
searchParams: Promise<{ page?: string }>
}) {
return (
<>
<Suspense fallback={null}>
<CurrentPage searchParams={searchParams} />
</Suspense>
<Link id="to-page-2" href="/search?page=2">
To page 2
</Link>
</>
)
}
'use client'
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.
export function ThirdPartyPush() {
useEffect(() => {
if ((window as any).__injectThirdPartyPush) {
window.history.pushState({ thirdParty: true }, '', '/?tp=1')
}
}, [])
return null
}
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