Skip to content

fix(react-router): scroll with revalidation #13671

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/kind-paws-try.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-router": patch
---

Skip scroll restoration on useRevalidator() calls because they're not new locations
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
- decadentsavant
- developit
- dgrijuela
- DimaAmega
- DigitalNaut
- dmitrytarassov
- dokeet
Expand Down
40 changes: 40 additions & 0 deletions packages/react-router/__tests__/router/scroll-restoration-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,46 @@ describe("scroll restoration", () => {
expect(t.router.state.restoreScrollPosition).toBe(50);
expect(t.router.state.preventScrollReset).toBe(false);
});

it("does not restore scroll on revalidation", async () => {
let t = setup({
routes: SCROLL_ROUTES,
initialEntries: ["/"],
});

expect(t.router.state.restoreScrollPosition).toBe(null);
expect(t.router.state.preventScrollReset).toBe(false);

let positions = {};

// Simulate scrolling to 100 on /
let activeScrollPosition = 100;
t.router.enableScrollRestoration(positions, () => activeScrollPosition);

// Revalidate
let R = await t.revalidate();
await R.loaders.index.resolve("INDEX");

expect(t.router.state.restoreScrollPosition).toBe(false);
expect(t.router.state.preventScrollReset).toBe(false);

// Scroll to 200
activeScrollPosition = 200;

// Go to /tasks
let nav1 = await t.navigate("/tasks");
await nav1.loaders.tasks.resolve("TASKS");

expect(t.router.state.restoreScrollPosition).toBe(null);
expect(t.router.state.preventScrollReset).toBe(false);

// Restore on pop back to /
let nav2 = await t.navigate(-1);
expect(t.router.state.restoreScrollPosition).toBe(null);
await nav2.loaders.index.resolve("INDEX");
expect(t.router.state.restoreScrollPosition).toBe(200);
expect(t.router.state.preventScrollReset).toBe(false);
});
});

describe("scroll reset", () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-router/lib/dom/lib.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2077,7 +2077,7 @@ export function useScrollRestoration({
// Restore scrolling when state.restoreScrollPosition changes
// eslint-disable-next-line react-hooks/rules-of-hooks
React.useLayoutEffect(() => {
// Explicit false means don't do anything (used for submissions)
// Explicit false means don't do anything (used for submissions or revalidations)
if (restoreScrollPosition === false) {
return;
}
Expand Down
12 changes: 7 additions & 5 deletions packages/react-router/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ export interface RouterState {
/**
* Current scroll position we should start at for a new view
* - number -> scroll position to restore to
* - false -> do not restore scroll at all (used during submissions)
* - false -> do not restore scroll at all (used during submissions/revalidations)
* - null -> don't have a saved position, scroll to hash or top of page
*/
restoreScrollPosition: number | false | null;
Expand Down Expand Up @@ -1269,6 +1269,11 @@ export function createRouter(init: RouterInit): Router {
blockers.forEach((_, k) => blockers.set(k, IDLE_BLOCKER));
}

// Don't restore on router.revalidate()
let restoreScrollPosition = isUninterruptedRevalidation ?
false :
getSavedScrollPosition(location, newState.matches || state.matches);

// Always respect the user flag. Otherwise don't reset on mutation
// submission navigations unless they redirect
let preventScrollReset =
Expand Down Expand Up @@ -1337,10 +1342,7 @@ export function createRouter(init: RouterInit): Router {
initialized: true,
navigation: IDLE_NAVIGATION,
revalidation: "idle",
restoreScrollPosition: getSavedScrollPosition(
location,
newState.matches || state.matches
),
restoreScrollPosition,
preventScrollReset,
blockers,
},
Expand Down