SvelteStack / svelte-query

Performant and powerful remote data synchronization for Svelte
https://sveltequery.vercel.app
MIT License
813 stars 31 forks source link

Best practise SvelteKit #83

Open finnstrand opened 2 years ago

finnstrand commented 2 years ago

What is the best practice of using Svelte Query with SvelteKit?

First, I tried to use useQuery inside an endpoint, but I got the error message Function called outside component initialization.

Then I used the data from the endpoint as initalData. But then the endpoint is called every time I load the page even if there is a cache.

export let accounts

const result = useQuery('accounts', getAccounts, {
  initialData: accounts,
})

Now I'm trying queryClient.setQueryData('accounts', accounts) inside the endpoint. But the endpoint doesn't have access to the same queryClient as the front-end so I guess that won't work. I also get a Cannot read property 'fallthrough' of undefined error.

Any tips?

Thanks!