Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Intermittent scroll restoration issues when using code-splitting #1816

Open
mbise1993 opened this issue Mar 2, 2024 · 0 comments
Open

Intermittent scroll restoration issues when using code-splitting #1816

mbise1993 opened this issue Mar 2, 2024 · 0 comments
Labels
react Related to the react adapter

Comments

@mbise1993
Copy link

Version:

  • @inertiajs/react version: 1.0.14

Describe the problem:

Scroll restoration fails intermittently on back navigation when using code-splitting (i.e. resolving components asynchronously). I've observed this both with browser-default scroll restoration and by forcing usage of Intertia's scroll restoration mechanism by adding the scroll-region attribute to my page's scrolling element.

I've already spent some time digging into this (and asked about it in the Discord awhile back) and from what I can tell, the issue is a race condition between when the component we're navigating back to finishes rendering and when the scroll restoration mechanism executes. It seems that when using async components, it is possible for the the browser/Inertia to attempt to restore the scroll position before the component has completely loaded and rendered. I've verified this by switching my app to use eager loading for component resolution - after that change, the scroll position is restored as expected on back navigations. I'm not sure if this issue appears with other frameworks as well, but I imagine it could since they are also using Vite.

The simplest solution I've found is wrapping the scrollTo() call and scrollTop/scrollLeft setters in the router in requestAnimationFrame or setTimeout(() => {...}, 0). I'm not sure if this is a fool-proof solution, but its worked so far in my testing. I'd be happy to submit a PR for whatever solution you think would be best.

Steps to reproduce:

  1. Clone the reproduction repo here: https://github.com/mbise1993/inertia-scroll-restoration-demo
  2. Follow the instructions in the README to run the app and see the error
@mbise1993 mbise1993 added the react Related to the react adapter label Mar 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
react Related to the react adapter
Projects
None yet
Development

No branches or pull requests

1 participant