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: create new URL when calling goto(...), to handle case where URL is mutated #13196

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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/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 @@ -1741,7 +1741,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));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no big deal but thought maybe it's worth saving creating another instance if one was already created.

Suggested change
url = new URL(resolve_url(url));
const resolved = resolve_url(url);
// force new URL for page.url reactivity
url = url === resolved ? new URL(resolved) : resolved;


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
Loading