Skip to content

scrollRestoration not working the first time #3680

Closed

Description

@anthony2261

Which project does this relate to?

Router

Describe the bug

With scrollRestoration: true, the scroll won't get reset on first page load, but after opening another page via <Link> or similar, it will work. I think the bug might not always happen.

Your Example Website or App

https://stackblitz.com/edit/github-yeosauxb?file=src%2Fmain.tsx

Steps to Reproduce the Bug or Issue

  • Open stackblitz app, scroll to the bottom of the home page (which load by default)
  • Click on Click Me
  • It should open the bottom of the About page, even though scrollRestoration is enabled
  • If the top of the About page opens, try again by going to the home page, refreshing the app, and repeating the steps above

Expected behavior

I expect the page I just navigated to to start from the top, but instead my scroll is at the bottom.

Screenshots or Videos

router-issue-recording.mp4

Platform

  • OS: WSL2
  • Browser: Edge, Firefox
  • Version: 1.112.13

Additional context

No response

Activity

  1. schiller-manuel commented on Mar 6, 2025

    @schiller-manuel
    Collaborator

    please provide a complete minimal example by forking one of the existing examples on stackblitz

  2. changed the title [-]resetScroll not working the first time with StrictMode[/-] [+]scrollRestoration not working the first time with StrictMode[/+] on Mar 7, 2025
  3. anthony2261 commented on Mar 7, 2025

    @anthony2261
    ContributorAuthor

    @schiller-manuel added stackblitz example, reproduction steps and a video 🤝

  4. changed the title [-]scrollRestoration not working the first time with StrictMode[/-] [+]scrollRestoration not working the first time[/+] on Mar 7, 2025
  5. anthony2261 commented on Mar 7, 2025

    @anthony2261
    ContributorAuthor

    Edited the issue: also happens with strict mode off.

  6. Krasnopir commented on Mar 17, 2025

    @Krasnopir

    i have another case, when scroll restoration does not scroll to top when i trigger navigate to the same url second and more times.
    I have a button with click handler, where i make some post request to get id, and then navigate to /checkout/${id}
    after history.back() and trying to press that button again i see that my checkout page is not on the top.

    I find that it start from 1.112.13.
    In 1.112.12 it works fine

    So maybe this commit broke some logic ?

    https://github.com/TanStack/router/pull/3659/files

  7. JoshuaHD commented on Mar 20, 2025

    @JoshuaHD

    i have another case, when scroll restoration does not scroll to top when i trigger navigate to the same url second and more times. I have a button with click handler, where i make some post request to get id, and then navigate to /checkout/${id} after history.back() and trying to press that button again i see that my checkout page is not on the top.

    I find that it start from 1.112.13. In 1.112.12 it works fine

    So maybe this commit broke some logic ?

    https://github.com/TanStack/router/pull/3659/files

    I can confirm the issue. @Krasnopir Thank you for pointing out the working version. I rolled back to 1.112.12 after trying the entire day way the scroll behavior was so weird and random.

  8. added theissue type on Mar 20, 2025
  9. denys-isaichenko commented on Apr 2, 2025

    @denys-isaichenko
    Contributor

    It seems like there's an issue here https://github.com/TanStack/router/blob/main/packages/react-router/src/Match.tsx#L144-L157 , resolvedLocation is pointing to the previous location instead of the new one in some cases when ref callback executed

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions