Closed Cluster2a closed 1 year ago
Changing $page.url
then calling goto()
seems to exhibit buggy behaviour.
It works as expected if you simply call goto()
without modifying the page store.
// calling goto() will change the $page.url.searchParams for you
await goto(`?page=${pageNumber}`);
Working example below: https://stackblitz.com/edit/sveltejs-kit-template-default-c7qtfl?file=src/routes/+page.svelte
I'm wondering why $page.url
isn't read-only, and if it is meant to be modifiable, why doesn't sveltekit sync those changes with the browser address bar?
Changing
$page.url
then callinggoto()
seems to exhibit buggy behaviour. It works as expected if you simply callgoto()
without modifying the page store.// calling goto() will change the $page.url.searchParams for you await goto(`?page=${pageNumber}`);
Working example below: https://stackblitz.com/edit/sveltejs-kit-template-default-c7qtfl?file=src/routes/+page.svelte
I'm wondering why
$page.url
isn't read-only, and if it is meant to be modifiable, why doesn't sveltekit sync those changes with the browser address bar?
Actually, I am not setting the store, I set the searchParams. https://developer.mozilla.org/en-US/docs/Web/API/URL/searchParams
$page.url
should be seen as readonly, so changing it should have no effects. Creating a readonly-version of the built-in URL object sounds cumbersome and would add needless bytes to the resulting bundle. I'm therefore inclined to close this.
Alternatives:
I don't understand, is this a bad practice ?
// https://blabla.com?page=2
// +page.svelte
const addParams = (num) => {
const newUrl = new URL($page.url);
newUrl.searchParams.set(other, 'hello');
goto(newUrl);
}
<button on:click={() => addParams()} />
I expected to go to https://blabla.com?page=2&other=hello
but it's not the case
I don't understand, is this a bad practice ?
// https://blabla.com?page=2 // +page.svelte const addParams = (num) => { const newUrl = new URL($page.url); newUrl.searchParams.set(other, 'hello'); goto(newUrl); } <button on:click={() => addParams()} />
I expected to go to
https://blabla.com?page=2&other=hello
but it's not the case
This is completely fine and does work. Example: https://stackblitz.com/edit/sveltejs-kit-template-default-c7qtfl?file=src%2Froutes%2F%2Bpage.svelte Append a search param to the current page, then click one of the buttons to see the new URL.
Mea culpa, it was a bug in my code ! Sorry for the confusion.
Describe the bug
When changing the searchparams via links, the load function runs on each change.
If the parameter is changed via
goto
only the first goto triggers the load.As a workaround, I could call invalidate manually.
Reproduction
https://codesandbox.io/p/sandbox/charming-einstein-yvkztt
Logs
No response
System Info
Severity
annoyance
Additional Information
No response