Skip to content

Conversation

wlemahieu
Copy link

🎯 Changes

Fix scroll position persistence in useWindowVirtualizer when navigating between routes

This PR removes the automatic initialOffset: () => window.scrollY from useWindowVirtualizer that was causing
scroll position to persist incorrectly when navigating between routes in single-page applications.

The Problem

When using useWindowVirtualizer in a SPA with client-side routing:

  1. User scrolls down on Route A (non-virtualized content)
  2. User navigates to Route B (virtualized list)
  3. Bug: Route B starts at the same scroll position as Route A instead of at the top

Open issues:

#997
TanStack/router#4107

The Solution

  • Removed the default initialOffset: () => window.scrollY from useWindowVirtualizer
  • The virtualizer now uses the default initialOffset of 0 (from virtual-core)
  • Users can still explicitly set initialOffset when needed (e.g., for SSR)

Tests Added

  • Test verifying useWindowVirtualizer doesn't set initialOffset from window.scrollY by default
  • Test verifying users can still explicitly set initialOffset when needed

This change aligns with the API documentation which states that initialOffset is "usually only useful if you are
rendering the virtualizer in a SSR environment."

Checklist

Copy link

changeset-bot bot commented Aug 14, 2025

⚠️ No Changeset found

Latest commit: b11ebde

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant