SvelteStack / svelte-query

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

[Vite] Function called outside of component initialization #49

Closed geoffreymugnier closed 3 years ago

geoffreymugnier commented 3 years ago

Edit : my example is stupid, it lacks a provider. I will have a better example when I get home

Hi, first let me start by saying that the library is great. I've been using it in classic svelte projects and it works like a charm. This week I started working on a Vite project and... well this time, it's not really working.

When I call useQuery in component initialization, it just throws "Function called outside component initialization"

How to reproduce:

git clone https://github.com/geoffreymugnier/vite-svelte-query-crash.git cd vite-svelte-query-crash npm i npm run dev

Repo : https://github.com/geoffreymugnier/vite-svelte-query-crash Am I doing something wrong ?

geoffreymugnier commented 3 years ago

The problem was not related to svelte-query but to @sveltejs/vite-plugin-svelte From the docs :

When importing any third-party libraries that uses Svelte's lifecycle API, e.g. onMount, setContext, and others, they need to be excluded from Vite's dependency pre-bundling process (https://www.npmjs.com/package/@sveltejs/vite-plugin-svelte)

Added this to my vite.config and everything's okay.

optimizeDeps: {
  exclude: ['@sveltestack/svelte-query']
}