-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
page.url not reactive on the new $app/state module. #13187
Comments
according to the docs Working Reproduction SvelteLab this works or goto(`?${url.searchParams.toString()}`, {
replaceState: true,
noScroll: true,
keepFocus: true,
invalidateAll: true,
}); this doesn't: goto(url, {
replaceState: true,
noScroll: true,
keepFocus: true,
invalidateAll: true,
}); |
The snippet below with a new URLSearchParams object works because we're not mutating the $page/state URL object <script>
import { page } from "$app/state";
import { goto } from "$app/navigation";
const currentPage = $derived(page.url.searchParams.get("page"));
</script>
<div>
<label for="current_page">Current Page:</label>
<input
id="current_page"
bind:value={() => page.url.searchParams.get("page"),
(value) => {
const searchParams = new URLSearchParams(page.url.search);
if (value) {
searchParams.set("page", value);
} else {
searchParams.delete("page");
}
goto(`?${searchParams}`, {
replaceState: true,
noScroll: true,
keepFocus: true,
invalidateAll: true,
});
}}
/>
</div>
<h1>Current Page: {currentPage}</h1> or creating a new URL object <script>
import { page } from "$app/state";
import { goto } from "$app/navigation";
const currentPage = $derived(page.url.searchParams.get("page"));
</script>
<div>
<label for="current_page">Current Page:</label>
<input
id="current_page"
bind:value={() => page.url.searchParams.get("page"),
(value) => {
const url = new URL(page.url);
if (value) {
url.searchParams.set("page", value);
} else {
url.searchParams.delete("page");
}
goto(url, {
replaceState: true,
noScroll: true,
keepFocus: true,
invalidateAll: true,
});
}}
/>
</div>
<h1>Current Page: {currentPage}</h1> As an aside, it's kind of a similar request to #13178 where people want $page/state to be mutable whereas I think it wasn't intended to work this way EDIT: actually, it is reactive but it's just not working with the proxied URL. Doing |
This is probably because when the URL is not actually updating by reference, |
@dummdidumm @Rich-Harris was the page.url created as why not use
|
while that's a nice idea, we can't do that at least in SvelteKit 2 while we still support Svelte 4, because we would need to import it from somewhere that does not exist in Svelte 4. |
but how are we using But if you mean that the behavior in SvelteKit 2 with Svelte v4 vs v5 will be different (reactive vs not), yeah.... I mean, it could be documented and people with Svelte 5 can start benefitting from this without having to wait for SvelteKit version 3. |
That's not why. These values are readonly — if we use |
@Rich-Harris Hey! Just to clarify, I don't expect variables using // This should not work
const currentPage = $derived(page.url.searchParams.get('page') || '');
page.url.searchParams.set('page', newPage); // This should work
const currentPage = $derived(page.url.searchParams.get('page') || '');
page.url.searchParams.set('page', newPage);
goto(page.url, { replaceState: true, noScroll: true, keepFocus: true, invalidateAll: true }) Also, while currently <!-- This works -->
<a data-sveltekit-replacestate data-sveltekit-noscroll data-sveltekit-keepfocus href="/my-page?page=1">1</a> |
Not sure I understand because at least client-side, while |
The intention is that these values are read-only. https://svelte.dev/docs/kit/$app-state
For example, updating |
@Gonzalo9823 agree, it's a totally reasonable expectation. We can fix it just by ensuring that |
Describe the bug
When you try to hear for changes on
page.url
using$derived
it won't work. For example If ones has a+page.svelte
that wants the value of asearchParams
like this:And then you update the
searchParams
usinggoto
:The value of
currentPage
won't be updated it. I tried multiple combinations of this, and the only thing that works is listening to another value ofpage
. For example something like this:Now if I change the
searchParams
mycurrentPage
will be updated to the correct value.Also this used to work using the
$app/store
module:Reproduction
https://replit.com/@GonzaloCaballe1/Reproduction
+page.svelte:
Logs
No response
System Info
Severity
annoyance
The text was updated successfully, but these errors were encountered: