Skip to content

Commit ff363eb

Browse files
authored
Fix client side router issue with handling hash (#3757)
1 parent bc3ea7d commit ff363eb

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

.changeset/chatty-ducks-shop.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
Prevent full reload when router navigates and only removes hash

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,11 @@ export class Router {
184184
// Ignore if <a> has a target
185185
if (a instanceof SVGAElement ? a.target.baseVal : a.target) return;
186186

187-
// Check if new url only differs by hash
188-
if (url.href.split('#')[0] === location.href.split('#')[0]) {
187+
// Check if new url only differs by hash and use the browser default behavior in that case
188+
// This will ensure the `hashchange` event is fired
189+
// Removing the hash does a full page navigation in the browser, so make sure a hash is present
190+
const [base, hash] = url.href.split('#');
191+
if (hash && base === location.href.split('#')[0]) {
189192
// Call `pushState` to add url to history so going back works.
190193
// Also make a delay, otherwise the browser default behaviour would not kick in
191194
setTimeout(() => history.pushState({}, '', url.href));

0 commit comments

Comments
 (0)