examples/svelte/basic-sveltekit
| Name | Last commit message | Last commit date |
|---|---|---|
| .. | ||
README.md
TanStack Svelte Query Basic Example (SvelteKit)#
A list of posts and a post detail view. Posts you have already opened are marked as visited from the query cache. The cache is persisted to localStorage.
Built with SvelteKit.
How it works#
src/lib/services/posts/queries.tsdefinespostsQueries.list(limit)andpostsQueries.detail(id)withqueryOptions, keyed by['posts', 'list', limit]and['posts', 'detail', id].src/lib/services/posts/api.tsfetches the posts from JSONPlaceholder.src/lib/Posts.sveltereads the first 10 posts withcreateQueryand callsclient.getQueryData(postsQueries.detail(post.id).queryKey)to style links whose detail is already cached.src/routes/[postId]/+page.tsis aloadfunction that only parsespostIdfrom the URL.src/routes/[postId]/+page.sveltepasses it tosrc/lib/Post.svelte, which reads the post withcreateQuery.src/routes/+layout.sveltewraps the app inPersistQueryClientProviderwith acreateAsyncStoragePersisteronlocalStorage, and setsenabled: browserso queries only run in the browser.
To run this example:
npm installnpm run dev