Skip to content

Commit

Permalink
fix: popstate listener was not removed on Router unmount
Browse files Browse the repository at this point in the history
  • Loading branch information
CatchABus authored Jun 16, 2021
1 parent 7a7ba80 commit efccb59
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions Router.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -454,18 +454,20 @@ let previousScrollState = null
// Update history.scrollRestoration depending on restoreScrollState
$: history.scrollRestoration = restoreScrollState ? 'manual' : 'auto'
const popStateChanged = (event) => {
// If this event was from our history.replaceState, event.state will contain
// our scroll history. Otherwise, event.state will be null (like on forward
// navigation)
if (event.state && event.state.__svelte_spa_router_scrollY) {
previousScrollState = event.state
}
else {
previousScrollState = null
}
}
if (restoreScrollState) {
window.addEventListener('popstate', (event) => {
// If this event was from our history.replaceState, event.state will contain
// our scroll history. Otherwise, event.state will be null (like on forward
// navigation)
if (event.state && event.state.__svelte_spa_router_scrollY) {
previousScrollState = event.state
}
else {
previousScrollState = null
}
})
window.addEventListener('popstate', popStateChanged)
afterUpdate(() => {
// If this exists, then this is a back navigation: restore the scroll position
Expand Down Expand Up @@ -588,5 +590,6 @@ const unsubscribeLoc = loc.subscribe(async (newLoc) => {
onDestroy(() => {
unsubscribeLoc()
window.removeEventListener('popstate', popStateChanged)
})
</script>

0 comments on commit efccb59

Please sign in to comment.