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 diff --git a/packages/kit/src/runtime/client/client.js b/packages/kit/src/runtime/client/client.js index 352daca6cd55..7594286af239 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.ownerDocument.getElementById(hash)?.scrollIntoView(); + return; + } // set this flag to distinguish between navigations triggered by // clicking a hash link and those triggered by popstate hash_navigating = true;