Skip to content

Missing window entry in scroll cache causes wrong scroll restoration #4782

Closed

Description

@KirianCaumes

Which project does this relate to?

Router

Describe the bug

When I start my application and land on the homepage, it seems that there is no window scroll entry saved in the scroll restoration cache.
As a result, if I navigate to another page, scroll down, then go back to the homepage, the scroll position is incorrectly restored — instead of resetting to scroll position 0, it retains the scroll position from the second page.

I’ve seen other issues related to scroll restoration, but this doesn’t seem to be exactly the same problem:

Your Example Website or App

https://github.com/KirianCaumes/TanStack-Router-ScrollRestoration-Bug

Steps to Reproduce the Bug or Issue

  1. Run the application, go to the home page (without scrolling)
  2. Go to About page
  3. Scroll a bit
  4. Go back to previous page

Expected behavior

When I go back to the previous page, it should go back to the previous position (0,0) instead of the current page position.

Screenshots or Videos

Image Image Image Image

Platform

  • Router: 1.129.8
  • OS: Windows (WSL)
  • Browser: Firefox
  • Browser Version: 141.0
  • Bundler: Vite
  • Bundler Version: 6.3.5

Additional context

It seems like the onScroll function in scroll-restoration.ts should be called during initialization, not only in response to scroll events via the event listener.
For example, if I manually call onScroll({ target: window }) right after the onScroll function is declared, the issue appears to be resolved.

Of course, that’s a dirty solution, but I think it demonstrates the root of the problem.

Activity

  1. added a commit that references this issue on Jul 26, 2025
    75624b8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions