Skip to content

Commit

Permalink
fix: correctly update $page.url.hash when navigating history (#10843)
Browse files Browse the repository at this point in the history
  • Loading branch information
eltigerchino authored Oct 9, 2023
1 parent 68fd488 commit f8aacaf
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/eleven-ads-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: correctly update `$page.url.hash` when navigating history
6 changes: 4 additions & 2 deletions packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1668,10 +1668,12 @@ export function create_client(app, target) {
if (event.state[INDEX_KEY] === current_history_index) return;

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

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

await navigate({
url: new URL(location.href),
url,
scroll,
keepfocus: false,
redirect_chain: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,9 @@ test.describe('Routing', () => {
await page.locator('[href="/routing/hashes/pagestore"]').click();
await expect(page.locator('#window-hash')).toHaveText('#target'); // hashchange doesn't fire for these
await expect(page.locator('#page-url-hash')).toHaveText('');
await page.goBack();
expect(await page.textContent('#window-hash')).toBe('#target');
expect(await page.textContent('#page-url-hash')).toBe('#target');
});

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

0 comments on commit f8aacaf

Please sign in to comment.