examples/svelte/contacts-sveltekit
| Name | Last commit message | Last commit date |
|---|---|---|
| .. | ||
README.md
TanStack Svelte Query Contacts Example (SvelteKit)#
A contacts app whose SvelteKit load functions read from the query cache and whose mutations invalidate it.
Built with SvelteKit.
How it works#
src/lib/services/contacts/queries.tsdefinescontactsQueries.list(query, customFetch)andcontactsQueries.detail(id, customFetch)withqueryOptions.src/lib/services/contacts/api.tscalls the/api/contactsendpoints with SvelteKit'sfetchwhen aloadfunction passes it.src/routes/+layout.tscreates theQueryClientin aloadfunction with a 10-secondstaleTimeandenabled: browser, andsrc/routes/+layout.svelteprovides it withQueryClientProvider.src/routes/(app)/+layout.ts,src/routes/(app)/contacts/[contactId]/+page.tsandsrc/routes/(app)/contacts/[contactId]/edit/+page.tsfill the cache in theirloadfunctions withqueryClient.query(...)andstaleTime: 'static', and the pages read the same options withcreateQuery.src/routes/(app)/+layout.sveltesubmits the search form throughcreateDebouncerfrom TanStack Pacer and shows a spinner whileuseIsFetchingcounts a fetching list query.- The form actions in the
+page.server.tsfiles change the contacts insrc/lib/server/contacts.ts.src/lib/ContactForm.svelteandsrc/routes/(app)/contacts/[contactId]/+page.svelteuseenhanceto callqueryClient.invalidateQueriesafter each submission.
To run this example:
npm installnpm run dev