This repo demonstrates how to use TanStack Query (Svelte Query) with Astro - something that isn't as straightforward as you might expect due to how Astro's island architecture works.
When trying to use Svelte Query with Astro, you'll likely run into this error:
Error: No QueryClient was found in Svelte context. Did you forget to wrap your component with QueryClientProvider?
This happens because Astro's islands are isolated JavaScript contexts, and Svelte's context API doesn't work across them.
We solved this by creating wrapper components that keep the QueryProvider and query components together in the same island:
Astro Page
└── Wrapper Component (client:only="svelte")
├── QueryProvider
└── Data Component (uses createQuery)
Here's a diagram showing how components are structured in this pattern:
client:only="svelte"
on the wrapper, not individual componentsTo use this pattern in your own project:
client:only="svelte"
getStaticPaths()
for known routesexport const prerender = false
for SSR (requires setup)Astro's partial hydration creates isolated "islands" of interactivity. Svelte's context API can only share context within a component tree, not across islands.
By wrapping related components together, we keep everything that needs to share context in the same island.
['posts']
, ['post', id]
staleTime
and cacheTime
values