Skip to content

Commit

Permalink
Merge pull request #225 from DimitrisRK/patch-1
Browse files Browse the repository at this point in the history
fix: `popstate` listener was not removed on Router unmount
  • Loading branch information
ItalyPaleAle authored Jun 16, 2021
2 parents 7a7ba80 + efccb59 commit a90e697
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 a90e697

Please sign in to comment.