How to Mutate URL without causing reload? #8540
The Problem:I have a sveltekit app where I need to display a list of entries. As with any list of entries, I need pagination, sorting and ordering. The problem is that I would like the url to reflect these "states" to make it easier to share the list with other people. So something like "www.foo.bar/baz?query=x&sort=y&order=y&page=0". My current solutionI currently have the following function
It works but I am not sure if this is the best way. Is there any way of achieving this in sveltekit? I would love to have a more native solution to the framework. Especially considering how I dont even know what the I just saw this online and pasted it into my project. Making a few changes here and there. |
Replies: 1 comment
I highly recommend using GET forms to implement this as SvelteKit will intercept and retain client-side navigation on form submission. <form action="/baz">
<label>
Query
<input name="query">
</label>
<label>
Sort
<input name="sort">
</label>
<label>
Order
<input name="order">
</label>
<label>
Page
<input name="page">
</label>
</form> Otherwise, you can manually call For example: |
I highly recommend using GET forms to implement this as SvelteKit will intercept and retain client-side navigation on form submission.
https://kit.svelte.dev/docs/form-actions#get-vs-post
Submitting the form will update the URL with the search parameters matching the input names and values.
Otherwise, you can manually call
goto
instead ofhistory.replaceState
is a good way to maintain client-side navigation (no reloads).For example:
https://st…