Skip to content

Commit ee7f889

Browse files
authored
Fix history push getting treated like replace when followed by refresh (#95392)
Currently this drops a history push: ```tsx router.push('/target') // this entry was getting lost router.refresh() ``` So you end up on `/target`, but the previous page's history entry was replaced instead of a new one being pushed. As a result, pressing Back yanks you past the previous page. It seems like a regression from #88046. I verified it's broken in 16.2, but it was working in 16.1. This fixes the regression. ## Test Plan New test fails before, works after. Also tried it in a small repro project. --- *Everything below is written by Claude.* The push happens when React commits a state whose `pushRef.pendingPush` is true (`HistoryUpdater`). Refresh-type actions (`refresh`, `hmr-refresh`, `server-patch`) create their state with a fresh `pushRef` where `pendingPush` is false. React can skip committing the superseded navigation state entirely, so `pendingPush: true` is never observed and the push never happens — `HistoryUpdater` replaces the previous page's history entry with the new URL instead of pushing a new one. Going back after that does nothing: the entry the browser returns to has no state, and the popstate handler ignores entries without state. The old router carried the previous state's `pendingPush` forward unless a reducer set it explicitly (https://github.com/vercel/next.js/blob/5ccc9078e0/packages/next/src/client/components/router-reducer/handle-mutable.ts#L41-L43), and the refresh reducer never set it. That guard was lost in #88046, which deleted `handleMutable` and moved the final state assembly into `completeSoftNavigation`, where `pushRef` is created fresh with `pendingPush: navigateType === 'push'`. This PR restores the old behavior there. There's no double push: if the navigation's state did commit, the push already happened and consumed the flag (`HistoryUpdater` mutates the `pushRef`), and its same-URL check turns a leftover flag into a replace anyway. In dev, an `hmr-refresh` dispatched while compiling the destination route on demand can supersede the navigation the same way. That's what's behind the recent `browser back to a revalidated page` flakes in `navigation.test.ts` (traced in #95385). <!-- NEXT_JS_LLM_PR -->
1 parent d1268a9 commit ee7f889

7 files changed

Lines changed: 120 additions & 1 deletion

File tree

‎packages/next/src/client/components/router-reducer/reducers/refresh-reducer.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ export function refreshDynamicData(
7878
UnknownDynamicStaleTime
7979
)
8080

81-
const navigateType = 'replace'
81+
// If the previous navigation hasn't pushed its history entry yet (React
82+
// hasn't committed its state), this refresh may commit in its place, so it
83+
// takes over the push. If the navigation does commit first, HistoryUpdater
84+
// sees that the URL already matches and replaces instead.
85+
const navigateType = state.pushRef.pendingPush ? 'push' : 'replace'
8286
return navigateToKnownRoute(
8387
now,
8488
state,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function Page() {
2+
return <h1>Target</h1>
3+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use client'
2+
3+
import { useRouter } from 'next/navigation'
4+
5+
export default function Page() {
6+
const router = useRouter()
7+
return (
8+
<div>
9+
<h1>Home</h1>
10+
<button
11+
id="push-and-refresh"
12+
onClick={() => {
13+
// Refresh in the same tick so the refresh state can supersede the
14+
// navigation's state before React commits it.
15+
router.push('/push-and-refresh/dest')
16+
router.refresh()
17+
}}
18+
>
19+
Push and refresh
20+
</button>
21+
</div>
22+
)
23+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function Page() {
2+
return <h1>Other</h1>
3+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use client'
2+
3+
import { useRouter } from 'next/navigation'
4+
5+
export default function Page() {
6+
const router = useRouter()
7+
return (
8+
<div>
9+
<h1>Home</h1>
10+
<button
11+
id="push-and-replace"
12+
onClick={() => {
13+
// The destination page suspends forever, so the push settles but
14+
// never commits. The replace then supersedes the pending push.
15+
router.push('/push-and-replace/suspended')
16+
setTimeout(() => {
17+
router.replace('/push-and-replace/other')
18+
}, 500)
19+
}}
20+
>
21+
Push and replace
22+
</button>
23+
</div>
24+
)
25+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use client'
2+
3+
import { use } from 'react'
4+
5+
const forever = new Promise<never>(() => {})
6+
7+
export default function Page() {
8+
if (typeof window !== 'undefined') {
9+
// Suspend the navigation transition forever so it never commits.
10+
use(forever)
11+
}
12+
return null
13+
}

‎test/e2e/app-dir/navigation/navigation.test.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,54 @@ describe('app dir - navigation', () => {
948948
})
949949
})
950950

951+
describe('browser back after a push followed by a refresh', () => {
952+
it('should load the previous page', async () => {
953+
const browser = await next.browser('/push-and-refresh')
954+
expect(await browser.elementByCss('h1').text()).toBe('Home')
955+
const historyLength = await browser.eval('window.history.length')
956+
957+
await browser.elementById('push-and-refresh').click()
958+
959+
await retry(async () => {
960+
expect(await browser.elementByCss('h1').text()).toBe('Target')
961+
})
962+
await retry(async () => {
963+
expect(await browser.eval('window.history.length')).toBe(
964+
historyLength + 1
965+
)
966+
})
967+
968+
await browser.back()
969+
970+
await retry(async () => {
971+
expect(await browser.elementByCss('h1').text()).toBe('Home')
972+
})
973+
})
974+
})
975+
976+
describe('replace during a push that has not committed', () => {
977+
it('should supersede the pending push', async () => {
978+
const browser = await next.browser('/push-and-replace')
979+
// Compile the destination pages so the push settles before the replace.
980+
await browser.eval(
981+
`Promise.all([
982+
fetch('/push-and-replace/suspended'),
983+
fetch('/push-and-replace/other'),
984+
]).then(() => 'ok')`
985+
)
986+
expect(await browser.elementByCss('h1').text()).toBe('Home')
987+
const historyLength = await browser.eval('window.history.length')
988+
989+
await browser.elementById('push-and-replace').click()
990+
991+
await retry(async () => {
992+
expect(await browser.elementByCss('h1').text()).toBe('Other')
993+
})
994+
expect(await browser.url()).toContain('/push-and-replace/other')
995+
expect(await browser.eval('window.history.length')).toBe(historyLength)
996+
})
997+
})
998+
951999
describe('middleware redirect', () => {
9521000
it('should change browser location when router.refresh() gets a redirect response', async () => {
9531001
const browser = await next.browser('/redirect-on-refresh/auth')

0 commit comments

Comments
 (0)