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

@sanity/svelte-loader's useQuery to support client side rendering #1756

Open
majoravery opened this issue Aug 19, 2024 · 0 comments
Open

@sanity/svelte-loader's useQuery to support client side rendering #1756

majoravery opened this issue Aug 19, 2024 · 0 comments
Assignees

Comments

@majoravery
Copy link

majoravery commented Aug 19, 2024

Is your feature request related to a problem? Please describe.
I am currently using the @sanity/svelte-loader package for my application and below is a very contrived version of the problem I'm facing:

// +page.server.ts
import { query, type Photo } from '$lib/sanity/queries';

export async function load(event) {
	const { loadQuery } = event.locals;
	const { slug } = event.params;

	const params = { slug };
	const initial = await loadQuery<Photo>(query, params);

	return {
		query,
		params,
		options: { initial }
	};
}
// src/routes/p/[slug]/+page.svelte.ts
<script lang="ts">
  import { useQuery } from "@sanity/svelte-loader";

  export let data;
  let q = useQuery(data);

  $: ({ data: item } = $q);
</script>

<article>
    <h1>{item.title}</h1>
    <p>{item.description}</p>
    {#each item.relevantItems as relevantItem}
      <a class="relevant-link" href={`/p/${relevantItem.slug}`}
    {/each}
</article>

This page works fine if it's being navigated to from other pages, but if I navigate to the same page type via a.relevant-link, the URL would be updated but my content would remain the same because of the $q store not being updated, ie data now receives the correct new slug from event.params, but the item destructured from $q is still of the first page.

Describe the solution you'd like
It would be good if the useQuery function or store returns a setter function that I can use to update the query params

Describe alternatives you've considered
I considered writing my own Svelte store to handle these queries so that I can do a .set() myself but after looking into the source code, it looks like there are a lot of other features in useQuery() that I might miss out on, so I stopped the idea.

Additional context

"@sanity/svelte-loader": "^1.11.36"
"svelte": "^4.2.7"
@rdunk rdunk self-assigned this Aug 19, 2024
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