AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

Router.Refresh() Scrolls To Top After History.ReplaceState Changed The Search Params (Tree-Mismatch Retry Uses ScrollBehavior.Default)

router.refresh() unexpectedly scrolls the page to the top after a history.replaceState call changes the search params. The root cause is a hard-coded ScrollBehavior.Default in the tree-mismatch retry path (serverPatchReducer) and in the non-redirect server action path (serverActionReducer), which overrides the NoScroll intent of the original refresh.

highConfidence 95%Next.jsAffected V16.2.3Affected V16.3.4Affected V16.4.0-Canary.19

Origin Analysis

After history.replaceState, the canonicalUrl changes but renderedSearch and tree remain old. router.refresh() fetches with the new canonicalUrl, causing a tree mismatch. The retry in serverPatchReducer hard-codes ScrollBehavior.Default, which creates a new CacheNode with a live ScrollRef, leading layout-router's handlePotentialScroll to scroll the document to top. The same hard-coded Default exists in serverActionReducer for actions without a redirect.
1. Install dependencies and start the Next.js app (production or dev). 2. Open the app and scroll down a few screens. 3. Click a button that calls window.history.replaceState(null, '', '?open=1') (documented native History API pattern). The URL updates and useSearchParams updates, but the page stays in place. 4. Click a button that calls router.refresh(). 5. Observe the page jumps to the top, documentElement.scrollTop becomes 0.

Fixing Code Block

Edge Case Audit

This hotfix may suppress scrolling in rare cases where a tree-mismatch retry legitimately should scroll (e.g., after a client-side navigation that triggers a retry). It could alter scroll behavior for server actions that previously scrolled to top by default. Thorough testing of navigation scroll behavior (including back/forward and server action flows) is required. Rollback: revert to hard-coded ScrollBehavior.Default in both reducers if regressions occur.

Ecosystem Topology