examples/solid/offline-tanstack-router
| Name | Last commit message | Last commit date |
|---|---|---|
| .. | ||
README.md
TanStack Solid 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. ThemutationFncancels the detail query throughcontext.client.cancelQueriesbefore posting the comment.src/App.tsxwraps the app inPersistQueryClientProviderwith acreateAsyncStoragePersisteronlocalStorageand a 24-hourgcTime.queryClient.setMutationDefaults(moviesQueries.all(), ...)registers the mutation function, so paused mutations restored after a reload can run, andonSuccesscallsqueryClient.resumePausedMutations(). It passesuseIsRestoring()to the router context.src/routes/index.tsxandsrc/routes/$movieId.tsxread the movies withuseQuery. The detail route'sloaderreturns cached data fromqueryClient.getQueryData, and otherwise callsqueryClient.query(...)only whenonlineManager.isOnline()istrueand the cache is not being restored.src/routes/$movieId.tsxupdates the comment withuseMutation, writing it to the cache inonMutatewithqueryClient.setQueryData, rolling back inonErrorand invalidating inonSettled. While offline the mutation is paused andisPausedistrue.src/mocks.tsuses MSW in the browser to serve/moviesand/movies/:idfrom an in-memory list with a 500 ms delay.
To run this example:
npm installnpm run dev