Skip to content

Commit 70589c0

Browse files
committed
[fix] update current.url on hashchange
Fixes #5693
1 parent 5e82693 commit 70589c0

File tree

4 files changed

+12
-1
lines changed

4 files changed

+12
-1
lines changed

.changeset/funny-cycles-travel.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] update current.url on hashchange

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,10 +1321,12 @@ export function create_client({ target, base, trailing_slash }) {
13211321
if (hash !== undefined && base === location.href.split('#')[0]) {
13221322
// set this flag to distinguish between navigations triggered by
13231323
// clicking a hash link and those triggered by popstate
1324+
// TODO why not update history here directly?
13241325
hash_navigating = true;
13251326

13261327
update_scroll_positions(current_history_index);
13271328

1329+
current.url = url;
13281330
stores.page.set({ ...page, url });
13291331
stores.page.notify();
13301332

packages/kit/test/apps/basics/src/routes/routing/hashes/pagestore/+page.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@
1717
<h1 id="window-hash">{hash}</h1>
1818
<h1 id="page-url-hash">{$page.url.hash}</h1>
1919

20-
<a href="#target">Nav to #ing with anchor tag</a>
20+
<a href="#target">Nav to hash</a>
21+
<a href="/routing/hashes/pagestore">Nav to page</a>
2122
<div id="target">Target</div>

packages/kit/test/apps/basics/test/client.test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,9 @@ test.describe('Routing', () => {
551551
await page.click('[href="#target"]');
552552
expect(await page.textContent('#window-hash')).toBe('#target');
553553
expect(await page.textContent('#page-url-hash')).toBe('#target');
554+
await page.click('[href="/routing/hashes/pagestore"]');
555+
await expect(page.locator('#window-hash')).toHaveText('#target'); // hashchange doesn't fire for these
556+
await expect(page.locator('#page-url-hash')).toHaveText('');
554557
});
555558

556559
test('does not normalize external path', async ({ page }) => {

0 commit comments

Comments
 (0)