diff --git a/.changeset/great-kangaroos-train.md b/.changeset/great-kangaroos-train.md new file mode 100644 index 000000000000..1d5d66c7936b --- /dev/null +++ b/.changeset/great-kangaroos-train.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +fix: update `$page.url` when URL hash is directly altered in address bar diff --git a/packages/kit/src/runtime/client/client.js b/packages/kit/src/runtime/client/client.js index a049d2d3eeaf..9b060a3ed6aa 100644 --- a/packages/kit/src/runtime/client/client.js +++ b/packages/kit/src/runtime/client/client.js @@ -1487,6 +1487,10 @@ export function create_client(app, target) { callbacks.before_navigate.forEach((fn) => fn(navigation)); } + const url = new URL(location.href, document.baseURI); + update_url_and_page_store_notifying_with_url(url); + + if (should_block) { e.preventDefault(); e.returnValue = ''; @@ -1570,9 +1574,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_and_page_store_notifying_with_url(url); return; } @@ -1719,6 +1721,15 @@ export function create_client(app, target) { stores.navigating.set(null); } }); + + /** + * @param {URL} url + */ + function update_url_and_page_store_notifying_with_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..74fe6039bd36 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 @@