-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Support for global query params that get preserved on navigation #734
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
Comments
Query string parameters should pertain to the page they are appended to - if you want to change global state based on them, then you can do as I do, and set a store value as soon as the query parameter has been set. You can do this by subscribing to the page store in your layout. Do you have a concrete use-case for passing the same query parameters to each page, which stops you using a global store or browser local storage? |
Storing the value in a store is not enough for my use case as I want it to be reflected in the url. The use cases are multiple. Here is an example : https://jolly-roger.eth.link/demo/?subgraph=https://api.thegraph.com/subgraphs/name/wighawag/purple-warlock-staging this is now a shareable url that you can use, navigate, then bookmark and share yourself. It will always use the same graphql endpoint. Basically once the query params are supposed to affect the app in a global manner, it is important to have it reflected in the url after navigation, so it is not being dependent on the context (which user, whether they have local storage, etc....) |
I wonder if we should close this as a duplicate of #560 which requests an |
I don't think it's exactly the same use case, at least if |
This can be achieved like so:
I think this is a sufficiently niche requirement that SvelteKit doesn't need to make it any more convenient than that. |
to clarify @Rich-Harris your solution does not fulfil the requirement stated in the original post because
I understand that this might not be wanted in the core of svelte/kit but a mechanism to support such feature (where you can continue using href like normal) would be great. @benmccann mentioned the use of onNavigate and I see that #3293 has been merged. I ll have a go and see if that can fulfil my need Thanks |
@wighawag Did you get this to work? I have the same situation and I do not want to modify every single |
@einarpersson I use my own url function that transform url like so
See : https://github.com/wighawag/jolly-roger/blob/main/web/src/lib/utils/url.ts It would be better if svelte kit allowed us to customize url behavior. Unfortunately The |
alright, I ended up using beforeNavigate(({ from, to, cancel }) => {
if (from.url.searchParams.has('foo') && !to.url.searchParams.has('foo')) {
cancel()
goto(to.url.pathname + `?foo=${from.url.searchParams.get('foo')}`)
}
}) |
Intesresting, was thinking to try something along these lines, does it have undersired side effect or is it completely fine to use beforeNavigate like that ? and is svelte-kit going to support that use long term ? @benmccann what do you think ? |
Keep in mind that this does not work without javascript.
I don't see reason why sveltekit would stop supporting this. |
Is your feature request related to a problem? Please describe.
Currently I need to keep track of query string that are supposed to be used across the application and inject them in every link.
This is because svelte will take in consideration the given href and if that href do not contains any query string, then none will appear in the next navigation.
This is expected as this is the normal behaviour for browser. But it is often the case that query string are used as global config across the app and for it to not get reset on every navigation, we need to ensure it get added there every time. This is especially important in the context of static site.
This would be great if there was a way to inject these on navigation in svelte kit (maybe it is already possible somehow?)
An example of a router that support that quite easily is curi
Describe the solution you'd like
The solution that might make more sense is to let user of svelte kit transform navigation request. Like there could be a onNavigation callback where we can change the request.
How important is this feature to you?
Without it I would need to remove the use of
<a href
links and use a component that inject the global query params.The text was updated successfully, but these errors were encountered: