Open History to see the last commit
1# TanStack Svelte Query Batching Example
2β
3Several components request characters at the same time, and an async batcher from TanStack Pacer combines those requests into one.
4β
5## How it works
6β
7- `src/lib/services/characters/queries.ts` defines `charactersQueries.detail(id)` with `queryOptions`, keyed by `['characters', 'detail', id]` with a 1-minute `staleTime`.
8- `src/lib/services/characters/api.ts` adds each `getCharacter(id)` call to an `AsyncBatcher` from TanStack Pacer with `wait: 0`. The batcher sends one Rick and Morty API request for all collected ids and resolves each query's promise with its own character.
9- `src/lib/Characters.svelte` passes one `charactersQueries.detail(id)` per id to `createQueries`, so each character is still a separate query in the cache even though the network request is shared.
10- `src/App.svelte` parses the comma-separated ids and passes them to `Characters` when "Load characters" is clicked.
11β
12To run this example:
13β
14- `npm install`
15- `npm run dev`