examples/lit/offline-lit-labs-router
| Name | Last commit message | Last commit date |
|---|---|---|
| .. | ||
README.md
TanStack Lit Query Offline Example (@lit-labs/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 @lit-labs/router.
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/queryClient.tscreates theQueryClientand acreateAsyncStoragePersisterbacked bylocalStorage, and callsqueryClient.setMutationDefaults(moviesQueries.all(), ...)so persisted mutations can resume after a reload.src/main.tsrestores the cache withpersistQueryClientfrom@tanstack/query-persist-client-core, then callsqueryClient.resumePausedMutations()andinvalidateQueries(). Its@lit-labs/routerenterhook for/:movieIdloads the movie withqueryClient.query(...)when it is not cached andonlineManager.isOnline()istrue, and a button togglesonlineManager.setOnline.src/movie-list-view.tsandsrc/movie-detail-view.tsread the movies withcreateQueryController. The detail view'screateMutationControllersetsmutationKeyto the movie's query key, writes the comment optimistically inonMutate, restores it inonError, invalidates inonSettled, and shows when the mutationisPaused.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