examples/preact/offline-preact-iso
| Name | Last commit message | Last commit date |
|---|---|---|
| .. | ||
README.md
TanStack Preact Query Offline Example (preact-iso)#
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 preact-iso.
How it works#
src/services/movies/queries.tsdefinesmoviesQueries.list()andmoviesQueries.detail(id)withqueryOptions, andmoviesMutations.update()withmutationOptions. ItsmutationFncallscontext.client.cancelQueriesfor the movie before posting the comment.src/App.tsxwraps the app inPersistQueryClientProviderwith acreateAsyncStoragePersisterbacked bylocalStorage, andqueryClient.setMutationDefaults(moviesQueries.all(), ...)registers the mutation function so persisted mutations can resume after a reload. ItsonSuccesscallsqueryClient.resumePausedMutations()and theninvalidateQueries().Listinsrc/App.tsxusesuseQuery(moviesQueries.list())and prefetches a movie on hover withqueryClient.query(moviesQueries.detail(movie.id)), only whenonlineManager.isOnline()istrueanduseIsRestoring()isfalse.Detailinsrc/App.tsxcallsuseMutationwithmutationKeyset to the movie's query key.onMutatecancels the query and writes the new comment withsetQueryData,onErrorrestores the previous data,onSettledinvalidates the movie, andisPausedshows when the mutation is waiting for the network.src/mocks.tsis an MSW handler for/moviesand/movies/:idwith a 500 ms delay. The POST handler saves the comment in uppercase.
To run this example:
npm installnpm run dev