From 8e548fd16210c65b378c341e544bde18d9c20f4d Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Thu, 25 May 2023 10:51:40 +0100 Subject: [PATCH 1/3] fix: prevent history change when clicking same hash link --- packages/kit/src/runtime/client/client.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/packages/kit/src/runtime/client/client.js b/packages/kit/src/runtime/client/client.js index eeaa007f91e2..8c5c54475ecb 100644 --- a/packages/kit/src/runtime/client/client.js +++ b/packages/kit/src/runtime/client/client.js @@ -1539,6 +1539,15 @@ export function create_client(app, target) { // Removing the hash does a full page navigation in the browser, so make sure a hash is present const [nonhash, hash] = url.href.split('#'); if (hash !== undefined && nonhash === location.href.split('#')[0]) { + // If we are trying to navigate to the same hash, we should only + // attempt to scroll to that element and avoid any history changes. + // Otherwise, this can cause Firefox to incorrectly assign a null + // history state value without any signal that we can detect. + if (current.url.hash === url.hash) { + event.preventDefault(); + a.scrollIntoView(); + return; + } // set this flag to distinguish between navigations triggered by // clicking a hash link and those triggered by popstate hash_navigating = true; From 43bcedb5d9980115c5dc005c3479b4c33419c852 Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Thu, 25 May 2023 11:05:43 +0100 Subject: [PATCH 2/3] Include changeset --- .changeset/cool-snakes-join.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/cool-snakes-join.md diff --git a/.changeset/cool-snakes-join.md b/.changeset/cool-snakes-join.md new file mode 100644 index 000000000000..f14ffdefd06d --- /dev/null +++ b/.changeset/cool-snakes-join.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +fix: prevent history change when clicking same hash link From ba4354dbe5c971db271a3ac9b24cf777c66fda83 Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Thu, 25 May 2023 19:53:31 +0100 Subject: [PATCH 3/3] Fix bug --- packages/kit/src/runtime/client/client.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/src/runtime/client/client.js b/packages/kit/src/runtime/client/client.js index 8c5c54475ecb..a94c58c350d0 100644 --- a/packages/kit/src/runtime/client/client.js +++ b/packages/kit/src/runtime/client/client.js @@ -1545,7 +1545,7 @@ export function create_client(app, target) { // history state value without any signal that we can detect. if (current.url.hash === url.hash) { event.preventDefault(); - a.scrollIntoView(); + a.ownerDocument.getElementById(hash)?.scrollIntoView(); return; } // set this flag to distinguish between navigations triggered by