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

README.md

18 lines1.8 KB

1# TanStack Vue Query Offline Example (TanStack 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 [TanStack Router](https://tanstack.com/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 mutation function cancels the movie's detail query through `context.client.cancelQueries` before sending the comment.
10- `src/routes/$movieId.component.vue` reads the movie with `useQuery` and shows a message when `isPaused` is `true` with no data. Its `useMutation` sets `mutationKey` to the movie's query key, updates the cache in `onMutate`, rolls back in `onError`, and shows "mutation paused - offline" while the mutation is paused.
11- `src/routes/$movieId.ts` prefetches the movie with `queryClient.query` in the route `loader`, but only when it is not cached, `onlineManager.isOnline()` is `true`, and the cache is not being restored. `src/router.ts` passes the `queryClient` to the router as context.
12- `src/queryClient.ts` creates the `QueryClient` with a 24-hour `gcTime`, a `localStorage` persister from `createAsyncStoragePersister`, and `queryClient.setMutationDefaults(moviesQueries.all(), ...)` so mutations paused offline still have a `mutationFn` after a reload. `src/main.ts` passes `persistQueryClient` to `VueQueryPlugin` as `clientPersister`, and once the cache is restored, `clientPersisterOnSuccess` calls `resumePausedMutations` and then `invalidateQueries`.
13- `src/mocks.ts` is an MSW worker that serves `/movies` and `/movies/:id` with a 500 ms delay and stores comments in uppercase.
14​
15To run this example:
16​
17- `npm install`
18- `npm run dev`