Skip to content
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

Add Option to Prevent URL Updates on Initial Render #143

Open
fromaline opened this issue Nov 28, 2024 · 5 comments
Open

Add Option to Prevent URL Updates on Initial Render #143

fromaline opened this issue Nov 28, 2024 · 5 comments

Comments

@fromaline
Copy link

fromaline commented Nov 28, 2024

Describe the problem

Hey, thanks for the great library! It’s super handy for working with query parameters in SvelteKit.

I noticed that when you set a defaultValue, it updates the URL on the initial render if the parameter isn’t already there. For example:

const count = queryParam('count', {
  encode: (value: number) => value.toString(),
  decode: (value: string | null) => (value ? parseInt(value) : null),
  defaultValue: 10,
});

If I load /, it immediately changes the URL to /?count=10.
It would be nice to have an option to apply the default value without modifying the URL right away.

Describe the proposed solution

Maybe something like:

const count = queryParam('count', {
  encode: (value: number) => value.toString(),
  decode: (value: string | null) => (value ? parseInt(value) : null),
  defaultValue: 10,
  applyDefaultValueSilently: true,
});

This way, count would still default to 10 internally, but the URL stays clean until the user interacts with it.

Let me know if this makes sense or if there’s already a way to do this. Thanks again for your hard work on this library!

@fromaline
Copy link
Author

@paoloricciuti, hey!
Just a gentle bump on the above 👆

@paoloricciuti
Copy link
Owner

@paoloricciuti, hey! Just a gentle bump on the above 👆

Uh sorry, i need to check but i'm pretty sure you can already do something...it should be something like showDefaults set to false but i want to read your question better (whenever i have time 😅)

@fromaline
Copy link
Author

@paoloricciuti, thanks for your response! I tried setting showDefaults to false, but I’m still seeing the query parameter added to the URL on the first load.

@paoloricciuti
Copy link
Owner

Sorry but i need an actual reproduction then...i tried and it's working for me.

@fromaline
Copy link
Author

@paoloricciuti, got it! I'll try to provide the working example ASAP!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants