Skip to content

Commit

Permalink
Fix client side router issue with handling hash (#3757)
Browse files Browse the repository at this point in the history
  • Loading branch information
PH4NTOMiki authored Feb 7, 2022
1 parent bc3ea7d commit ff363eb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/chatty-ducks-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Prevent full reload when router navigates and only removes hash
7 changes: 5 additions & 2 deletions packages/kit/src/runtime/client/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,11 @@ export class Router {
// Ignore if <a> has a target
if (a instanceof SVGAElement ? a.target.baseVal : a.target) return;

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

0 comments on commit ff363eb

Please sign in to comment.