Open History to see the last commit
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`