Skip to content

Commit

Permalink
[fix] update current.url on hashchange
Browse files Browse the repository at this point in the history
Fixes #5693
  • Loading branch information
dummdidumm committed Sep 14, 2022
1 parent 5e82693 commit 70589c0
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/funny-cycles-travel.md
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

[fix] update current.url on hashchange
2 changes: 2 additions & 0 deletions packages/kit/src/runtime/client/client.js
Expand Up @@ -1321,10 +1321,12 @@ export function create_client({ target, base, trailing_slash }) {
if (hash !== undefined && base === location.href.split('#')[0]) {
// set this flag to distinguish between navigations triggered by
// clicking a hash link and those triggered by popstate
// TODO why not update history here directly?
hash_navigating = true;

update_scroll_positions(current_history_index);

current.url = url;
stores.page.set({ ...page, url });
stores.page.notify();

Expand Down
Expand Up @@ -17,5 +17,6 @@
<h1 id="window-hash">{hash}</h1>
<h1 id="page-url-hash">{$page.url.hash}</h1>

<a href="#target">Nav to #ing with anchor tag</a>
<a href="#target">Nav to hash</a>
<a href="/routing/hashes/pagestore">Nav to page</a>
<div id="target">Target</div>
3 changes: 3 additions & 0 deletions packages/kit/test/apps/basics/test/client.test.js
Expand Up @@ -551,6 +551,9 @@ test.describe('Routing', () => {
await page.click('[href="#target"]');
expect(await page.textContent('#window-hash')).toBe('#target');
expect(await page.textContent('#page-url-hash')).toBe('#target');
await page.click('[href="/routing/hashes/pagestore"]');
await expect(page.locator('#window-hash')).toHaveText('#target'); // hashchange doesn't fire for these
await expect(page.locator('#page-url-hash')).toHaveText('');
});

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

0 comments on commit 70589c0

Please sign in to comment.