What is Svelte Query?
Svelte Query brings all the power of TanStack Query to Svelte 5, leveraging Svelte’s modern reactivity system with runes ($state, $derived, $effect) to provide:
- Automatic background refetching - Keep your data fresh without manual intervention
- Window focus refetching - Automatically update when users return to your app
- Request deduplication - Eliminate redundant network requests
- Optimistic updates - Update UI instantly before server confirmation
- Built-in caching - Intelligent cache management with automatic garbage collection
- Pagination & infinite scroll - First-class support for complex data loading patterns
- TypeScript support - Full type inference and type safety
- DevTools - Visualize and debug your queries in real-time
Core Concepts
Svelte Query provides reactive primitives that integrate seamlessly with Svelte 5’s rune-based reactivity:Queries
Queries are used for fetching data. They automatically cache results and handle refetching, error states, and loading states.Mutations
Mutations are used for creating, updating, or deleting data. They provide callbacks for side effects and optimistic updates.Infinite Queries
Infinite queries handle paginated data that can be loaded incrementally.Key Features
Svelte Query is built specifically for Svelte 5 and uses modern Svelte runes for optimal reactivity and performance.
Reactive by Default
All query results are reactive and automatically update your UI when data changes. The library uses Svelte 5’s$state and $derived runes internally for fine-grained reactivity.
Type-Safe
Full TypeScript support with automatic type inference:Automatic Refetching
Data automatically refetches when:- Component mounts (configurable)
- Window regains focus
- Network reconnects
- Custom refetch intervals
Smart Caching
Queries are cached by theirqueryKey and shared across your application:
Architecture
Svelte Query follows a provider-based architecture:createQuery, createMutation, and other hooks which will automatically access the QueryClient from context.
Performance
Svelte Query is designed for optimal performance:- Request deduplication - Multiple components requesting the same data result in a single network request
- Automatic garbage collection - Unused queries are automatically removed from cache
- Structural sharing - Query results are structurally shared to minimize re-renders
- Fine-grained reactivity - Only components using specific query properties re-render when those properties change
Comparison with Other Solutions
| Feature | Svelte Query | Manual Fetch | Svelte Stores |
|---|---|---|---|
| Caching | ✅ Automatic | ❌ Manual | ❌ Manual |
| Background Refetching | ✅ Built-in | ❌ Manual | ❌ Manual |
| Request Deduplication | ✅ Automatic | ❌ None | ❌ Manual |
| TypeScript | ✅ Full Support | ⚠️ Partial | ⚠️ Partial |
| DevTools | ✅ Yes | ❌ No | ❌ No |
| Optimistic Updates | ✅ Built-in | ❌ Manual | ❌ Manual |
| Pagination | ✅ First-class | ❌ Manual | ❌ Manual |
Next Steps
Install Svelte Query
Get started by installing the package and setting up your first query client.Installation Guide →
Quick Start
Learn the basics with a step-by-step tutorial.Quick Start →
TypeScript
Configure TypeScript for maximum type safety.TypeScript Guide →
DevTools
Install and configure the Svelte Query DevTools.DevTools Setup →