Skip to content

Commit 4aa976e

Browse files
authored
fix: prevent history change when clicking same hash link (#10032)
* fix: prevent history change when clicking same hash link * Include changeset * Fix bug
1 parent 7499d8f commit 4aa976e

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

.changeset/cool-snakes-join.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
fix: prevent history change when clicking same hash link

packages/kit/src/runtime/client/client.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1539,6 +1539,15 @@ export function create_client(app, target) {
15391539
// Removing the hash does a full page navigation in the browser, so make sure a hash is present
15401540
const [nonhash, hash] = url.href.split('#');
15411541
if (hash !== undefined && nonhash === location.href.split('#')[0]) {
1542+
// If we are trying to navigate to the same hash, we should only
1543+
// attempt to scroll to that element and avoid any history changes.
1544+
// Otherwise, this can cause Firefox to incorrectly assign a null
1545+
// history state value without any signal that we can detect.
1546+
if (current.url.hash === url.hash) {
1547+
event.preventDefault();
1548+
a.ownerDocument.getElementById(hash)?.scrollIntoView();
1549+
return;
1550+
}
15421551
// set this flag to distinguish between navigations triggered by
15431552
// clicking a hash link and those triggered by popstate
15441553
hash_navigating = true;

0 commit comments

Comments
 (0)