Repository navigation
Description
Duplicates
- I have searched the existing issues
Latest version
- I have tested the latest version
Current behavior 😯
@solidjs/router does no scroll restoration of its own. createRouter.ts only calls scrollToHash (scroll to top) on push navigations, and back/forward is left entirely to the browser's native same-document restoration heuristic.
That heuristic is unreliable. If the destination route forces a synchronous layout after mount, while the document is still short, Chrome clamps and loses the saved offset for the previous history entry, and the deferred restore never runs. Going back leaves you at the top of a long page instead of where you were.
Expected behavior 🤔
Back/forward returns to the previous scroll position, regardless of what the intermediate route does during mount. This is what the browser does for MPA navigation, and what TanStack Start does for SPA navigation by restoring explicitly.
Steps to reproduce 🕹
Two routes. /long is a tall page linking to /short:
// routes/long.tsx
import { For } from "solid-js";
export default function Long() {
return (
<For each={Array.from({ length: 100 })}>
{(_, i) => (
<div style={{ padding: "80px", border: "4px solid #b45309" }}>
<a href="/short">block {i()}</a>
</div>
)}
</For>
);
}// routes/short.tsx
import { onMount } from "solid-js";
export default function Short() {
onMount(() => {
void document.body.offsetHeight; // any forced layout read is enough
});
return <div>short page</div>;
}- Open
/long, scroll down to around y=3000. - Click a link to
/short. - Press back.
You land at y=0. Delete the onMount body and it restores to y=3000 correctly.
Note when writing a test for this: Chromium's headless shell does no native scroll restoration at all, so every case fails there and it cannot distinguish good from bad. It needs headed or full Chromium.
Context 🔦
Came out of triaging solidjs/solid-start#2016, where scroll restoration broke after adding an Embla carousel to the destination page. Embla turned out to be incidental. I hooked scrollTo, scrollBy, scrollIntoView, focus, scroll, and the scrollTop/scrollLeft setters, and confirmed Embla never scrolls anything. It just measures its viewport on init.
Bisect over what the destination route does on mount, verified on @solidjs/start 2.0.0-rc.3 with @solidjs/router 0.15.4 in headed Chromium:
| Destination route does | scrollY after back |
|---|---|
| nothing | 3000 ✅ |
document.body.offsetHeight read |
0 ❌ |
el.focus() |
0 ❌ |
el.scrollLeft = 0 |
0 ❌ |
window.scrollTo(0, 0) |
0 ❌ |
Embla carousel (also with all watch* disabled) |
0 ❌ |
forced layout in onCleanup instead of onMount |
3000 ✅ |
| rAF loop / ResizeObserver / IntersectionObserver | 3000 ✅ |
CSS transform / overflow: hidden |
3000 ✅ |
Navigating via <a noscroll> fails identically, so the router's own scrollTo(0, 0) on push is not the trigger.
A manual restoration component fixes all ten variants above, which suggests the router could own this rather than deferring to the browser:
import { useBeforeLeave, useLocation } from "@solidjs/router";
import { createEffect, onMount } from "solid-js";
export function ScrollRestoration() {
const location = useLocation();
onMount(() => (history.scrollRestoration = "manual"));
useBeforeLeave(() => sessionStorage.setItem(`sr:${location.pathname}`, String(window.scrollY)));
createEffect(() => {
const saved = Number(sessionStorage.getItem(`sr:${location.pathname}`) || 0);
requestAnimationFrame(() => requestAnimationFrame(() => window.scrollTo(0, saved)));
});
return null;
}That sketch is keyed on pathname and restores on push as well as pop, so it is a workaround rather than a proposal. A real implementation should key on the history entry and only restore on traversal.
Your environment 🌎
@solidjs/router: 0.15.4
@solidjs/start: 2.0.0-rc.3
solid-js: 1.9.11
Chromium 1208 (headed), macOS 15
Activity
Add opt-in scroll restoration for back/forward navigation (#577)
Merge main (0.16.3): scroll restoration, default-on for 1.0 (#577)
ryansolid commentedon Jul 27, 2026
Shipped in both lines:
<Router scrollRestoration>.next(ae57803): on by default with the default browser history; opt out withscrollRestoration: falsein thecreateRouterconfig. Customhistoryadapters own their session and opt in explicitly.Implementation follows your analysis:
history.scrollRestoration = "manual", positions captured continuously per history entry (keyed by the depth the router already stamps foruseBeforeLeave, persisted to sessionStorage across reloads), restored once the traversal's navigation settles. If the destination commits shorter than the target offset — the forced-layout scenario you isolated — a ResizeObserver retries as the document grows, cancelled by the first user scroll, so the position is restored a beat later instead of lost.Scope is window scroll only; per-element restoration and layout-shift compensation are out. Thanks for the unusually thorough diagnosis — the instrumentation ruling Embla out saved this from being closed as a third-party bug.