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

README.md

18 lines1.6 KB

1# TanStack Solid Query Offline Example (Solid Router)
2​
3A movie list that keeps working offline. Queries and mutations are persisted, mutations made offline are paused, and they resume when the connection returns or after a reload.
4​
5Built with [Solid Router](https://docs.solidjs.com/solid-router).
6​
7## How it works
8​
9- `src/services/movies/queries.ts` defines `moviesQueries.list()` and `moviesQueries.detail(id)` with `queryOptions`, and `moviesMutations.update()` with `mutationOptions`. The `mutationFn` cancels the detail query through `context.client.cancelQueries` before posting the comment.
10- `src/App.tsx` wraps the app in `PersistQueryClientProvider` with a `createAsyncStoragePersister` on `localStorage` and a 24-hour `gcTime`. `queryClient.setMutationDefaults(moviesQueries.all(), ...)` registers the mutation function, so paused mutations restored after a reload can run, and `onSuccess` calls `queryClient.resumePausedMutations()`.
11- `src/routes/list.tsx` and `src/routes/detail.tsx` read the movies with `useQuery`. The detail route's `preload` calls `queryClient.query(...)` only when the data is not cached, `onlineManager.isOnline()` is `true` and the cache is not being restored.
12- `src/routes/detail.tsx` updates the comment with `useMutation`, writing it to the cache in `onMutate` with `queryClient.setQueryData`, rolling back in `onError` and invalidating in `onSettled`. While offline the mutation is paused and `isPaused` is `true`.
13- `src/mocks.ts` uses MSW in the browser to serve `/movies` and `/movies/:id` from an in-memory list with a 500 ms delay.
14​
15To run this example:
16​
17- `npm install`
18- `npm run dev`