nextjs - 💡(How to fix) Fix Bug: `history.pushState` to a new URL reverts when route has a prefetched `<Link>` + `proxy.ts` cookie-based redirect

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

Calling history.pushState to navigate to a new URL on the same route appears to succeed (URL updates, RSC fetch fires) but then silently reverts to the previous URL — when the same page contains:

  1. A <Link> whose href includes the URL state being changed (so Next prefetches it and caches a router entry keyed by the original URL).
  2. A useEffect that writes the URL state to a cookie on every change.
  3. A proxy.ts (formerly middleware.ts) that NextResponse.redirects when that URL parameter is absent, restoring it from the cookie.

Each piece in isolation is fine. Together they break history.pushState. Removing any one of the three fixes it.

Root Cause

Calling history.pushState to navigate to a new URL on the same route appears to succeed (URL updates, RSC fetch fires) but then silently reverts to the previous URL — when the same page contains:

  1. A <Link> whose href includes the URL state being changed (so Next prefetches it and caches a router entry keyed by the original URL).
  2. A useEffect that writes the URL state to a cookie on every change.
  3. A proxy.ts (formerly middleware.ts) that NextResponse.redirects when that URL parameter is absent, restoring it from the cookie.

Each piece in isolation is fine. Together they break history.pushState. Removing any one of the three fixes it.

Fix Action

Fix / Workaround

Suspected cause

Next.js monkey-patches history.pushState. The patched implementation fires an ACTION_RESTORE (or similar) action that consults the router prefetch cache.

Code Example

git clone https://github.com/asanoop24/nextjs-pushstate-prefetch-revert-repro
cd nextjs-pushstate-prefetch-revert-repro
pnpm install
pnpm build
pnpm start

---

Operating System:
  Platform: linux
Binaries:
  Node: 22.x
  pnpm: 9.x
Relevant Packages:
  next: 16.1.7
  react: 19.2.4
  react-dom: 19.2.4
Next.js Config:
  output: N/A
RAW_BUFFERClick to expand / collapse

Link to the code that reproduces this issue

https://github.com/asanoop24/nextjs-pushstate-prefetch-revert-repro

To Reproduce

git clone https://github.com/asanoop24/nextjs-pushstate-prefetch-revert-repro
cd nextjs-pushstate-prefetch-revert-repro
pnpm install
pnpm build
pnpm start
  1. Open http://localhost:3000.
  2. Click any period button (e.g. 7d). This seeds the last-period cookie.
  3. Click a different period button (e.g. 90d). The URL flashes to ?period=90d and immediately reverts back to ?period=7d.
  4. The server-rendered <pre> block confirms the RSC tree is for the old URL.

Current vs. Expected behavior

Expected behavior

URL stays on the new value; RSC re-fetches for the new URL.

Actual behavior

URL reverts to the previous value within ~1 frame. RSC tree never reflects the click.

Provide environment information

Operating System:
  Platform: linux
Binaries:
  Node: 22.x
  pnpm: 9.x
Relevant Packages:
  next: 16.1.7
  react: 19.2.4
  react-dom: 19.2.4
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Middleware, Linking and Navigating, Runtime, Dynamic Routes

Which stage(s) are affected? (Select all that apply)

next start (local), Other (Deployed)

Additional context

Reproduction: https://github.com/asanoop24/nextjs-pushstate-prefetch-revert-repro

Summary

Calling history.pushState to navigate to a new URL on the same route appears to succeed (URL updates, RSC fetch fires) but then silently reverts to the previous URL — when the same page contains:

  1. A <Link> whose href includes the URL state being changed (so Next prefetches it and caches a router entry keyed by the original URL).
  2. A useEffect that writes the URL state to a cookie on every change.
  3. A proxy.ts (formerly middleware.ts) that NextResponse.redirects when that URL parameter is absent, restoring it from the cookie.

Each piece in isolation is fine. Together they break history.pushState. Removing any one of the three fixes it.

Suspected cause

Next.js monkey-patches history.pushState. The patched implementation fires an ACTION_RESTORE (or similar) action that consults the router prefetch cache.

Each <Link href="/?view=a&period=7d"> populates the prefetch cache with an entry keyed by that URL. When the user clicks a filter and the pushState lands on /?view=a&period=90d, the prefetch-cache lookup appears to fuzzy-match the old prefetched entry and "restore" its URL — undoing the user's change.

The proxy.ts redirect + cookie write amplify the trigger. Removing the redirect (the proxy.ts returning NextResponse.next() always) fixes it; removing the cookie write fixes it; removing prefetch={false} on the <Link> fixes it.

Possibly related: #92187 (router-cache regression in 16.x).

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

FAQ

Expected behavior

URL stays on the new value; RSC re-fetches for the new URL.

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING