Skip to content

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

Closed

Description

@xiaodan999

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

home-tab-background-flickering.mp4

Platform

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

Additional context

No response

Activity

  1. xiaohai-huang commented on Oct 23, 2024

    @xiaohai-huang
    Contributor

    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. xiaohai-huang commented on Oct 23, 2024

    @xiaohai-huang
    Contributor

    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. xiaohai-huang commented on Oct 26, 2024

    @xiaohai-huang
    Contributor

    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 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