Skip to content

Commit

Permalink
fix: prevent history change when clicking same hash link (#10032)
Browse files Browse the repository at this point in the history
* fix: prevent history change when clicking same hash link

* Include changeset

* Fix bug
  • Loading branch information
trueadm authored May 25, 2023
1 parent 7499d8f commit 4aa976e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/cool-snakes-join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: prevent history change when clicking same hash link
9 changes: 9 additions & 0 deletions packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 4aa976e

Please sign in to comment.