smolter
API

ScrollRestoration

Restore saved history positions and scroll new pages after their DOM commits.

Compatibility

ReactAvailable in this renderer's browser integration.
React SSRAvailable to this renderer's SSR integration; browser event handlers run after hydration.
PreactAvailable in this renderer's browser integration.
Preact SSRAvailable to this renderer's SSR integration; browser event handlers run after hydration.

Import from @kasperrt/smolter/preact/document @kasperrt/smolter/react/document

Signature

function ScrollRestoration(_props: ScriptProps): null;

Parameters

Accepts ScriptProps for compatibility, but currently does not use nonce or render a script. Requires application context and the navigation integration that records history keys and scroll positions.

Returns

Renders nothing. While mounted, sets history.scrollRestoration to manual and restores the previous setting when unmounted.

Example

Mount once in the root document layout, alongside the application content. mountApplication supplies the navigation integration.

import { ScrollRestoration, Scripts } from "@kasperrt/smolter/preact/document";

export function DocumentFooter() {
  return (
    <>
      <ScrollRestoration />
      <Scripts />
    </>
  );
}

Restoration order

Waits for an idle snapshot with the committed history key, then schedules a frame so lazy views can finish mounting. A saved POP position takes precedence. Otherwise a hash target scrolls into view; without one, preventScrollReset preserves the current position, and ordinary navigation scrolls to [0, 0]. Positions are held in memory for this router rather than persisted across document reloads.

View implementation ↗