Open jhwheeler opened 1 year ago
edit: I mis read that you already used goto but will leave this answer here in case someone else finds it helpful
You can use the 'goto' function provided by Sveltekit to handle this.
import { goto } from "$app/navigation";
function updateQueryParams() {
queryParams = 'newParams'
const params = new URLSearchParams(window.location.search)
params.set('q', queryParams)
goto(`?${params.toString()}`);
}
A better way of achieving this is to use an 'a' tag instead of a button. (you can style it to look like a button or just wrap the button in an 'a' tag and remove your click listener). This has some accessibility benefits and makes it easier to understand whats going on when you come back to the code in the future. and should also work if javascript is disabled.
<script>
import { page } from '$app/stores'
import { browser } from '$app/environment'
let queryParams = ''
function updateQueryParams() {
queryParams = 'newParams'
const params = new URLSearchParams(window.location.search)
params.set('q', queryParams)
return `?${params.toString()}`
}
$: currentSearchParams = browser ? Object.fromEntries($page.url.searchParams) : null
$: console.log('currentSearchParams', currentSearchParams)
</script>
<a href={updateQueryParams()}>Update Query Params</a>
<p>Query Params: {queryParams}</p>
{#if browser}
<p>currentSearchParams: {JSON.stringify(currentSearchParams)}</p>
{/if}
You can use goto with keepfocus set to true:
<script>
import { page } from "$app/stores";
import { browser } from "$app/environment";
import { goto } from "$app/navigation";
let queryParams = "";
function updateQueryParams(e) {
const params = new URLSearchParams($page.url.searchParams);
params.set("q", e.target.value);
goto(`?${params.toString()}`, { keepFocus: true });
}
$: currentSearchParams = browser
? Object.fromEntries($page.url.searchParams)
: null;
$: console.log("currentSearchParams", currentSearchParams);
</script>
<!-- <button on:click={updateQueryParams}>Update Query Params</button> -->
<input type="text" on:input={updateQueryParams} />
<p>Query Params: {queryParams}</p>
{#if browser}
<p>currentSearchParams: {JSON.stringify(currentSearchParams)}</p>
{/if}
The issue is that we cannot detect changes made by history.replaceState()
unless we proxy it (then we can update $page.url
). We should decide if we want to support the History API better or improve the documentation of the existing solutions (with a focus on people using goto
instead).
or using data-sveltekit-keepfocus
:
<!-- SvelteKit enhances the form behaviour with a client-side navigation for GET method forms -->
<form data-sveltekit-keepfocus>
<!-- Natively populates the URL search params with the input name and value-->
<input type="text" name="q" />
</form>
https://kit.svelte.dev/docs/link-options#data-sveltekit-keepfocus
Below is an example with both goto
and the form solution implemented.
I don't think goto is the solution. I currently use this with a "?q=" parameter to keep what the user is searching. The idea is doing this so if they go back or to the results, the search is kept the same. The issue with goto in this case is that ig user wants to go back, it will start deleting letter by letter on the q parameter (it won't update the textbox tho).
Will there be any solution to handle the history api? Or maybe I can update it manually. Thank you
My website is: soundicly.com
I'm currently using goto as mentioned, but I just have one issue with it. I'm listening for afterNavigation to update stuff, but when I press the back arrow until getting to / (without parameter), nothing triggers (not mount, beforeNavigation, afterNavegation)
The issue is that we cannot detect changes made by
history.replaceState()
unless we proxy it (then we can update$page.url
). We should decide if we want to support the History API better or improve the documentation of the existing solutions (with a focus on people usinggoto
instead).
With the new pushState
and replaceState
"proxies", this should be possible though?
But even when using the SvelteKit pushState
or replaceState
, the $page.url
won't update.
Describe the bug
When manually setting the query parameters in the URL using history, the
$page
store does not update correctly to reflect these changes.In my SvelteKit application, I have a function that updates the query parameters in the URL using the
history
API. However, after updating the query parameters, the $page store does not reflect these changes. This behavior is inconsistent with the expected behavior, where the$page
store should update to reflect the current state of the page, including the query parameters.I also attempted to use
goto
to work around this, but the issue in my application is that the URL search params are being updated from a search input, and when yougoto
, the focus is lost on the input. Manually handling refocusing the input is possible, but quite the hassle.My current workaround is to not rely on
$page.url
, and only usewindow.location
.Reproduction
Here is a repo you can clone so you can reproduce the issue: https://github.com/jhwheeler/sveltekit-search-params-issue
In this example, when you click the "Update Query Params" button, it updates the query parameters in the URL using the history API. However, the
$page
store does not update correctly to reflect these changes:Here is a GIF of the output:
System Info
Severity
serious, but I can work around it