examples/svelte/auto-refetching-sveltekit
| Name | Last commit message | Last commit date |
|---|---|---|
| .. | ||
README.md
TanStack Svelte Query Auto Refetching Example (SvelteKit)#
A todo list that polls the server with refetchInterval. Adding a todo or clearing the list invalidates the query.
Built with SvelteKit.
How it works#
src/lib/services/todos/queries.tsdefinestodosQueries.list()withqueryOptions, keyed by['todos', 'list'], andtodosMutations.create()andtodosMutations.deleteAll()withmutationOptions.src/lib/services/todos/api.tscalls/api/data.src/routes/+page.sveltepasses the list options tocreateQuerywithrefetchIntervalbound to the interval input, so changing the input changes how often the query refetches. Both mutations callinvalidateQuerieson the list inonSuccess.src/routes/+layout.sveltecreates theQueryClientwithenabled: browser, so queries only run in the browser.src/routes/api/data/+server.tsis a SvelteKit endpoint that keeps the list in server memory and adds or clears items based on theaddandclearsearch params.
To run this example:
npm installnpm run dev