Skip to content
Source
Sign inSign up
TanStack/router

`<ScrollRestoration />` does not restore the scroll positions before DOM paint #2601

Closedxiaodan999(GitHub profile, opens in a new tab) opened this issue 3 comments

Which project does this relate to?

Router

Describe the bug

It shows the top of the page then restore the scroll positions. This causes flickering.

Your Example Website or App

https://stackblitz.com/edit/tanstack-router-k6ncqr?file=src%2Fmain.tsx

Steps to Reproduce the Bug or Issue

  1. At the / route (Home tab), scroll to the bottom of the home tab.
  2. Click the About tab.
  3. Click the Home tab.
  4. I can see the red background of the Home tab title appear and then suddenly disappeared.

Expected behavior

No red background flickering

Screenshots or Videos

https://github.com/user-attachments/assets/22f05924-bb58-4894-9938-5a7903b51db4

Platform

  • OS: WInd
  • Browser: Edge
  • Version: 130.0.2849.46

Additional context

No response

  1. xiaohai-huang(GitHub profile, opens in a new tab)commented

    I identified that the issue occurs because the onResolved event is triggered after the <HomeTab /> component has mounted. Since the <ScrollRestoration /> component performs scroll restoration when the onResolved event is emitted, it consistently restores the cached scroll position after <HomeTab /> becomes visible to the user.

    I resolved this issue using a workaround. Here’s how I addressed it:

    With the above change, the onResolved event will be emitted before the useLayoutEffect in <Page /> component. And the flash is gone

    Image

  2. I understand that the above workaround is not a real solution to this issue. As isTransitioning is obviously useful, removing it might cause some serious bugs in the other places. Hope we can find a real solution to this problem.

  3. I propose a solution which is to make the router emit a new event called onBeforePageMount.

    The onBeforePageMount is designed to trigger actions that should occur just before a new page component mounts. This event allows developers to perform pre-render operations, such as scroll restoration, ensuring that these actions complete before the page content becomes visible to the user.

Sign in to join this conversation.