paoloricciuti / sveltekit-search-params

The easiest way to read and WRITE from query parameters in sveltekit.
https://sveltekit-search-params.netlify.app
MIT License
478 stars 13 forks source link

Initial store value is undefined for param with defaultValue set #49

Closed dominikg closed 6 months ago

dominikg commented 8 months ago

Describe the bug

creating a store and immediately accessing it in the root layout while defining a parameter with a default value causes the initial value to be undefined.

In the reproduction, there is a parameter "foo" with default "bar". accessing /, the url is updated to include ?foo=bar but the initial value in +layout.svelte is undefined.

This is unexpected and causes an error if you want to access $params.foo.

Possible solutions:

Reproduction

https://www.sveltelab.dev/49is8k7v2xv3ap3

Logs

No response

ollema commented 8 months ago

maybe this is a separate feature request, but I think it would be really cool to not include default values in the URL at all. not upon initial page load, but also if they for some reason were set to their default value again in follow up page interactions.

the use case for this is a complex data table where there are lots of potential search params such as search filter params (q=), pagination params (page=,pageSize=), what columns to show (columns=)...

in those cases, it can be nice to show a clean URL if no filters actually have been applied. then that URL is easier to share. but if you for some reason have selected for example page 2 but then navigated to page 1 (which is the default), it would be nice if page 1 was pruned from the URL again so it is again easy to share

paoloricciuti commented 8 months ago

I'm a bit thorned about this, I want to think about the consequences of this. For example if you are reading from the load function without using the library it might be confusing. One of my ideas was to always reflect the url but I can see where having a default might help. I have to think about it