Skip to content

Commit

Permalink
fix: create new URL when calling goto(...), to handle case where UR…
Browse files Browse the repository at this point in the history
…L 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]>
  • Loading branch information
Rich-Harris and eltigerchino authored Dec 21, 2024
1 parent fe8c37c commit ed4d53e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/smooth-weeks-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: create new URL when calling `goto(...)`, to handle case where URL is mutated
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1800,7 +1800,7 @@ export function goto(url, opts = {}) {
throw new Error('Cannot call goto(...) on the server');
}

url = resolve_url(url);
url = new URL(resolve_url(url));

if (url.origin !== origin) {
return Promise.reject(
Expand Down
16 changes: 16 additions & 0 deletions packages/kit/test/apps/basics/src/routes/state/url/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<script>
import { page } from '$app/state';
import { goto } from '$app/navigation';
const q = $derived(page.url.searchParams.get('q') || undefined);
</script>

<button
type="button"
onclick={() => {
page.url.searchParams.set('q', 'test');
goto(page.url);
}}>test</button
>

<p>{`${q}`}</p>
7 changes: 7 additions & 0 deletions packages/kit/test/apps/basics/test/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,13 @@ test.describe('$app/state', () => {
await app.goto('/state/data/state-update/same-keys');
await expect(page.locator('p')).toHaveText('page.data was updated 1 time(s)');
});

test('page.url does update when used with goto', async ({ page }) => {
await page.goto('/state/url');
await expect(page.locator('p')).toHaveText('undefined');
await page.locator('button').click();
await expect(page.locator('p')).toHaveText('test');
});
});

test.describe('Invalidation', () => {
Expand Down

0 comments on commit ed4d53e

Please sign in to comment.