Skip to content

Commit ed4d53e

Browse files
fix: create new URL when calling goto(...), to handle case where URL is mutated (#13196)
* fix: create new URL when calling `goto(...)`, to handle case where URL is mutated * add test * format --------- Co-authored-by: Chew Tee Ming <[email protected]>
1 parent fe8c37c commit ed4d53e

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

.changeset/smooth-weeks-rhyme.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: create new URL when calling `goto(...)`, to handle case where URL is mutated

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1800,7 +1800,7 @@ export function goto(url, opts = {}) {
18001800
throw new Error('Cannot call goto(...) on the server');
18011801
}
18021802

1803-
url = resolve_url(url);
1803+
url = new URL(resolve_url(url));
18041804

18051805
if (url.origin !== origin) {
18061806
return Promise.reject(
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<script>
2+
import { page } from '$app/state';
3+
import { goto } from '$app/navigation';
4+
5+
const q = $derived(page.url.searchParams.get('q') || undefined);
6+
</script>
7+
8+
<button
9+
type="button"
10+
onclick={() => {
11+
page.url.searchParams.set('q', 'test');
12+
goto(page.url);
13+
}}>test</button
14+
>
15+
16+
<p>{`${q}`}</p>

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,13 @@ test.describe('$app/state', () => {
421421
await app.goto('/state/data/state-update/same-keys');
422422
await expect(page.locator('p')).toHaveText('page.data was updated 1 time(s)');
423423
});
424+
425+
test('page.url does update when used with goto', async ({ page }) => {
426+
await page.goto('/state/url');
427+
await expect(page.locator('p')).toHaveText('undefined');
428+
await page.locator('button').click();
429+
await expect(page.locator('p')).toHaveText('test');
430+
});
424431
});
425432

426433
test.describe('Invalidation', () => {

0 commit comments

Comments
 (0)