Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion packages/router/src/history/html5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ function useHistoryListeners(
return teardown
}

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

}

function beforeUnloadListener() {
const { history } = window
if (!history.state) return
Expand All @@ -140,15 +144,20 @@ function useHistoryListeners(
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

window.removeEventListener('pagehide', beforeUnloadListener)
document.removeEventListener('visibilitychange', beforeHiddenListener)
}

// set up the listeners and prepare teardown callbacks
window.addEventListener('popstate', popStateHandler)
// TODO: could we use 'pagehide' or 'visibilitychange' instead?
// https://developer.chrome.com/blog/page-lifecycle-api/
// note: iOS safari does not fire beforeunload, so we
// use pagehide and visibilitychange instead
window.addEventListener('beforeunload', beforeUnloadListener, {
passive: true,
})
window.addEventListener('pagehide', beforeUnloadListener)
document.addEventListener('visibilitychange', beforeHiddenListener)

return {
pauseListeners,
Expand Down