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

[Bug]: Scroll Restoration not working on page refresh in mobile safari #9463

Closed
fetters5 opened this issue Oct 14, 2022 · 3 comments · Fixed by #9945
Closed

[Bug]: Scroll Restoration not working on page refresh in mobile safari #9463

fetters5 opened this issue Oct 14, 2022 · 3 comments · Fixed by #9945
Labels

Comments

@fetters5
Copy link

What version of React Router are you using?

6.4.2

Steps to Reproduce

Scroll position is not maintained in mobile safari browsers upon reload of a page. This is due to using the beforeunload event which is not supported by ios safari

function useBeforeUnload(callback: () => any): void {
  React.useEffect(() => {
    window.addEventListener("beforeunload", callback);
    return () => {
      window.removeEventListener("beforeunload", callback);
    };
  }, [callback]);
}

By changing the above code to use the visibilitychange event and checking document.visibilityState === 'hidden' it seems to work as intended in safari

Reference material

https://developer.mozilla.org/en-US/docs/Web/API/Document/visibilitychange_event
https://www.igvita.com/2015/11/20/dont-lose-user-and-app-state-use-page-visibility/
https://levelup.gitconnected.com/how-to-send-an-asynchronous-request-at-the-end-of-a-page-session-90bf7229448c
https://tech.trivago.com/post/2020-11-17-exploringthepagevisibilityapifordetectin/

Expected Behavior

Expect that when reloading a page the scroll position will be maintained in ios safari

Actual Behavior

The scroll position goes to 0 since it was not captured.

@fetters5 fetters5 added the bug label Oct 14, 2022
@jakkku
Copy link
Contributor

jakkku commented Oct 29, 2022

Here is the codesandbox
please check it on IOS mobile safari

@github-actions
Copy link
Contributor

🤖 Hello there,

We just published version 6.7.1-pre.0 which involves this issue. If you'd like to take it for a test run please try it out and let us know what you think!

Thanks!

@brophdawg11 brophdawg11 added the awaiting release This issue have been fixed and will be released soon label Jan 24, 2023
@github-actions
Copy link
Contributor

🤖 Hello there,

We just published version 6.8.0 which involves this issue. If you'd like to take it for a test run please try it out and let us know what you think!

Thanks!

@brophdawg11 brophdawg11 removed the awaiting release This issue have been fixed and will be released soon label Jan 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants