Skip to content

Conversation

omnomnomapp
Copy link
Contributor

@omnomnomapp omnomnomapp commented Aug 3, 2025

fix #2536

Steps to reproduce

On iOS safari, scroll to the middle of the page, refresh, it'll go back to top instead of going back to the correct position.

What is expected?

It behaves properly in chrome for e.g., it'd restore the scroll position.

What is actually happening?

It's going back to the top of the page instead of restoring the scroll position.


The code window.addEventListener('beforeunload', beforeUnloadListener) does not trigger on iOS, we should use window.addEventListener('pagehide', ...) instead

Copy link

netlify bot commented Aug 3, 2025

Deploy Preview for vue-router canceled.

Name Link
🔨 Latest commit 4183104
🔍 Latest deploy log https://app.netlify.com/projects/vue-router/deploys/688f63e82173b200083fe091

Copy link
Member

@posva posva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

for (const teardown of teardowns) teardown()
teardowns = []
window.removeEventListener('popstate', popStateHandler)
window.removeEventListener('beforeunload', beforeUnloadListener)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can remove the beforeunload if we use pagehide

}

function beforeHiddenListener() {
document.hidden && beforeUnloadListener()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inline this function instead

@omnomnomapp
Copy link
Contributor Author

Provided another commit to fix those, thanks for the fast feedback :)

Copy link
Member

@posva posva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Could you test the ci release (should be up in a couple of minutes)

Copy link

pkg-pr-new bot commented Aug 3, 2025

Open in StackBlitz

npm i https://pkg.pr.new/vue-router@2537

commit: 4183104

@posva posva changed the title fix(router): use pagehide for iOS navigation (fix #2536) fix(router): use pagehide for iOS navigation Aug 3, 2025
@omnomnomapp
Copy link
Contributor Author

Yup looking good ! Just installed it and the patch works fine on iOS :) Thanks !

@posva posva merged commit e0e38ab into vuejs:main Aug 4, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Scroll is not restored after a refresh on iOS / mobile safari
2 participants