question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for global query params that get preserved on navigation

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
Rich-Harriscommented, Apr 6, 2022

This can be achieved like so:

<script>
  import { page } from '$app/stores';
</script>

<a href="/whatever{$page.url.search}">whatever</a>

I think this is a sufficiently niche requirement that SvelteKit doesn’t need to make it any more convenient than that.

1reaction
einarperssoncommented, Sep 8, 2022

@wighawag

alright, I ended up using beforeNavigate in +layout.svelte like this:

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')}`)
    }
  })
Read more comments on GitHub >

github_iconTop Results From Across the Web

A Neat Trick to Globally Preserve Query Params in Angular ...
It is based on simple axioms of web navigation. At the moment there is no way to preserve query params globally on all...
Read more >
A Neat Trick to Globally Preserve Query Params ... - Byte Limes
At the moment there is no way to preserve query params globally on all route navigations. For example, if you were checking for...
Read more >
How to keep query parameters in angular 5 on all routes by ...
Currently there is no way to set it globally. Using queryParamsHandling seems to be the only option:
Read more >
Preserve the current route's query parameters ... - Egghead.io
navigate or from within a component template via a [routerLink] directive, we may want to preserve the current route's query parameters and carry...
Read more >
Query Parameters - Routing - Ember Guides
Query params allow for additional application state to be serialized into the URL that can't otherwise fit into the path of the URL...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found