Skip to content

Commit f8aacaf

Browse files
authored
fix: correctly update $page.url.hash when navigating history (#10843)
1 parent 68fd488 commit f8aacaf

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

.changeset/eleven-ads-destroy.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: correctly update `$page.url.hash` when navigating history

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,10 +1668,12 @@ export function create_client(app, target) {
16681668
if (event.state[INDEX_KEY] === current_history_index) return;
16691669

16701670
const scroll = scroll_positions[event.state[INDEX_KEY]];
1671+
const url = new URL(location.href);
16711672

16721673
// if the only change is the hash, we don't need to do anything...
16731674
if (current.url.href.split('#')[0] === location.href.split('#')[0]) {
1674-
// ...except handle scroll
1675+
// ...except update our internal URL tracking and handle scroll
1676+
update_url(url);
16751677
scroll_positions[current_history_index] = scroll_state();
16761678
current_history_index = event.state[INDEX_KEY];
16771679
scrollTo(scroll.x, scroll.y);
@@ -1681,7 +1683,7 @@ export function create_client(app, target) {
16811683
const delta = event.state[INDEX_KEY] - current_history_index;
16821684

16831685
await navigate({
1684-
url: new URL(location.href),
1686+
url,
16851687
scroll,
16861688
keepfocus: false,
16871689
redirect_chain: [],

packages/kit/test/apps/basics/test/cross-platform/client.test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,9 @@ test.describe('Routing', () => {
641641
await page.locator('[href="/routing/hashes/pagestore"]').click();
642642
await expect(page.locator('#window-hash')).toHaveText('#target'); // hashchange doesn't fire for these
643643
await expect(page.locator('#page-url-hash')).toHaveText('');
644+
await page.goBack();
645+
expect(await page.textContent('#window-hash')).toBe('#target');
646+
expect(await page.textContent('#page-url-hash')).toBe('#target');
644647
});
645648

646649
test('back button returns to previous route when previous route has been navigated to via hash anchor', async ({

0 commit comments

Comments
 (0)