From 76a574d0d3bc4a089213b0e56e6ccf5f92d99c33 Mon Sep 17 00:00:00 2001 From: Paul Ebose Date: Wed, 21 Jun 2023 00:12:08 +0100 Subject: [PATCH 1/3] fix: update page store when URL hash is changed from the address bar --- packages/kit/src/runtime/client/client.js | 23 +++++++++++++++--- .../src/routes/store/data/+layout.svelte | 1 + packages/kit/test/apps/basics/test/test.js | 24 +++++++++++++++++++ 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/packages/kit/src/runtime/client/client.js b/packages/kit/src/runtime/client/client.js index 7594286af239..70d422a3c155 100644 --- a/packages/kit/src/runtime/client/client.js +++ b/packages/kit/src/runtime/client/client.js @@ -1554,9 +1554,7 @@ export function create_client(app, target) { update_scroll_positions(current_history_index); - current.url = url; - stores.page.set({ ...page, url }); - stores.page.notify(); + update_url(url); if (!options.replace_state) return; @@ -1670,6 +1668,16 @@ export function create_client(app, target) { delta }); } + // TODO: should this run regardless of the `event.state?.[INDEX_KEY]` if condition? + else { + // since popstate event is also emitted when an anchor referencing the same + // document is clicked, we have to check that the router isn't already handling + // the navigation. otherwise we would be updating the page store twice. + if (!hash_navigating) { + const url = new URL(location.href); + update_url(url); + } + } }); addEventListener('hashchange', () => { @@ -1701,6 +1709,15 @@ export function create_client(app, target) { stores.navigating.set(null); } }); + + /** + * @param {URL} url + */ + function update_url(url) { + current.url = url; + stores.page.set({ ...page, url }); + stores.page.notify(); + } }, _hydrate: async ({ diff --git a/packages/kit/test/apps/basics/src/routes/store/data/+layout.svelte b/packages/kit/test/apps/basics/src/routes/store/data/+layout.svelte index 1ad81a9ca762..ee85d2efaf6c 100644 --- a/packages/kit/test/apps/basics/src/routes/store/data/+layout.svelte +++ b/packages/kit/test/apps/basics/src/routes/store/data/+layout.svelte @@ -4,6 +4,7 @@
{JSON.stringify($page.data)}
{$page.error?.message}
+
{$page.url.hash}