examples/vue/offline-tanstack-router
| Name | Last commit message | Last commit date |
|---|---|---|
| .. | ||
README.md
TanStack Vue Query Offline Example (TanStack Router)#
A 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.
Built with TanStack Router.
How it works#
src/services/movies/queries.tsdefinesmoviesQueries.list()andmoviesQueries.detail(id)withqueryOptions, andmoviesMutations.update()withmutationOptions. The mutation function cancels the movie's detail query throughcontext.client.cancelQueriesbefore sending the comment.src/routes/$movieId.component.vuereads the movie withuseQueryand shows a message whenisPausedistruewith no data. ItsuseMutationsetsmutationKeyto the movie's query key, updates the cache inonMutate, rolls back inonError, and shows "mutation paused - offline" while the mutation is paused.src/routes/$movieId.tsprefetches the movie withqueryClient.queryin the routeloader, but only when it is not cached,onlineManager.isOnline()istrue, and the cache is not being restored.src/router.tspasses thequeryClientto the router as context.src/queryClient.tscreates theQueryClientwith a 24-hourgcTime, alocalStoragepersister fromcreateAsyncStoragePersister, andqueryClient.setMutationDefaults(moviesQueries.all(), ...)so mutations paused offline still have amutationFnafter a reload.src/main.tspassespersistQueryClienttoVueQueryPluginasclientPersister, and once the cache is restored,clientPersisterOnSuccesscallsresumePausedMutationsand theninvalidateQueries.src/mocks.tsis an MSW worker that serves/moviesand/movies/:idwith a 500 ms delay and stores comments in uppercase.
To run this example:
npm installnpm run dev