Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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