From 9028a3ff5476972d0f33746583c090089f477719 Mon Sep 17 00:00:00 2001 From: orange4glace Date: Fri, 12 Feb 2021 18:17:15 +0900 Subject: [PATCH 1/5] fix: #192 no override history state --- Router.svelte | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Router.svelte b/Router.svelte index 75e6aaa..9ab03f7 100644 --- a/Router.svelte +++ b/Router.svelte @@ -100,7 +100,7 @@ export async function push(location) { await tick() // Note: this will include scroll state in history even when restoreScrollState is false - history.replaceState({scrollX: window.scrollX, scrollY: window.scrollY}, undefined, undefined) + history.replaceState({...history.state, __svelte_spa_router_scrollX: window.scrollX, __svelte_spa_router_: window.scrollY}, undefined, undefined) window.location.hash = (location.charAt(0) == '#' ? '' : '#') + location } @@ -132,7 +132,10 @@ export async function replace(location) { const dest = (location.charAt(0) == '#' ? '' : '#') + location try { - window.history.replaceState(undefined, undefined, dest) + const newState = { ...history.state }; + delete newState['__svelte_spa_router_scrollX']; + delete newState['__svelte_spa_router_scrollY']; + window.history.replaceState(newState, undefined, dest) } catch (e) { // eslint-disable-next-line no-console @@ -193,7 +196,7 @@ function scrollstateHistoryHandler(event) { event.preventDefault() const href = event.currentTarget.getAttribute('href') // Setting the url (3rd arg) to href will break clicking for reasons, so don't try to do that - history.replaceState({scrollX: window.scrollX, scrollY: window.scrollY}, undefined, undefined) + history.replaceState({...history.state, __svelte_spa_router_scrollX: window.scrollX, __svelte_spa_router_scrollY: window.scrollY}, undefined, undefined) // This will force an update as desired, but this time our scroll state will be attached window.location.hash = href } From 864fbe5171956931ae0c426a41a022e57305430f Mon Sep 17 00:00:00 2001 From: orange4glace Date: Fri, 12 Feb 2021 18:19:23 +0900 Subject: [PATCH 2/5] fix: missing words --- Router.svelte | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Router.svelte b/Router.svelte index 9ab03f7..3018b74 100644 --- a/Router.svelte +++ b/Router.svelte @@ -100,7 +100,7 @@ export async function push(location) { await tick() // Note: this will include scroll state in history even when restoreScrollState is false - history.replaceState({...history.state, __svelte_spa_router_scrollX: window.scrollX, __svelte_spa_router_: window.scrollY}, undefined, undefined) + history.replaceState({...history.state, __svelte_spa_router_scrollX: window.scrollX, __svelte_spa_router_scrollY: window.scrollY}, undefined, undefined) window.location.hash = (location.charAt(0) == '#' ? '' : '#') + location } From 8351e670a529a15b6ffc4e5649ab5981eddde2b1 Mon Sep 17 00:00:00 2001 From: orange4glace Date: Fri, 12 Feb 2021 18:27:02 +0900 Subject: [PATCH 3/5] fix: linting --- Router.svelte | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Router.svelte b/Router.svelte index 3018b74..5440afa 100644 --- a/Router.svelte +++ b/Router.svelte @@ -132,9 +132,11 @@ export async function replace(location) { const dest = (location.charAt(0) == '#' ? '' : '#') + location try { - const newState = { ...history.state }; - delete newState['__svelte_spa_router_scrollX']; - delete newState['__svelte_spa_router_scrollY']; + const newState = { + ...history.state + } + delete newState['__svelte_spa_router_scrollX'] + delete newState['__svelte_spa_router_scrollY'] window.history.replaceState(newState, undefined, dest) } catch (e) { From 7794826fc86f3f76ebc253836514fb7482edc09d Mon Sep 17 00:00:00 2001 From: orange4glace Date: Fri, 12 Feb 2021 18:45:07 +0900 Subject: [PATCH 4/5] fix: missing replacements --- Router.svelte | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Router.svelte b/Router.svelte index 5440afa..616bd5b 100644 --- a/Router.svelte +++ b/Router.svelte @@ -433,7 +433,7 @@ if (restoreScrollState) { afterUpdate(() => { // If this exists, then this is a back navigation: restore the scroll position if (previousScrollState) { - window.scrollTo(previousScrollState.scrollX, previousScrollState.scrollY) + window.scrollTo(previousScrollState.__svelte_spa_router_scrollX, previousScrollState.__svelte_spa_router_scrollY) } else { // Otherwise this is a forward navigation: scroll to top From 2f485d6d47d7dfd238b43e086cc05719a69fe20a Mon Sep 17 00:00:00 2001 From: orange4glace Date: Sat, 13 Feb 2021 17:34:19 +0900 Subject: [PATCH 5/5] fix: missing replacements --- Router.svelte | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Router.svelte b/Router.svelte index 616bd5b..ecc3a66 100644 --- a/Router.svelte +++ b/Router.svelte @@ -422,7 +422,7 @@ if (restoreScrollState) { // 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.scrollY) { + if (event.state && event.state.__svelte_spa_router_scrollY) { previousScrollState = event.state } else {