Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correctly update $page.url.hash when navigating history #10843

Merged
merged 3 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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