Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

README.md

17 lines1.2 KB

1# TanStack Svelte Query Basic Example (SvelteKit)
2​
3A 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`.
4​
5Built with [SvelteKit](https://svelte.dev/docs/kit).
6​
7## How it works
8​
9- `src/lib/services/posts/queries.ts` defines `postsQueries.list(limit)` and `postsQueries.detail(id)` with `queryOptions`, keyed by `['posts', 'list', limit]` and `['posts', 'detail', id]`. `src/lib/services/posts/api.ts` fetches the posts from JSONPlaceholder.
10- `src/lib/Posts.svelte` reads the first 10 posts with `createQuery` and calls `client.getQueryData(postsQueries.detail(post.id).queryKey)` to style links whose detail is already cached.
11- `src/routes/[postId]/+page.ts` is a `load` function that only parses `postId` from the URL. `src/routes/[postId]/+page.svelte` passes it to `src/lib/Post.svelte`, which reads the post with `createQuery`.
12- `src/routes/+layout.svelte` wraps the app in `PersistQueryClientProvider` with a `createAsyncStoragePersister` on `localStorage`, and sets `enabled: browser` so queries only run in the browser.
13​
14To run this example:
15​
16- `npm install`
17- `npm run dev`