Apollo offline first support: Service Worker or Persisted Cache?
Asked Answered
W

1

12

I have a react app that use apollo as a grapql client. The app now need offline support for a subset/subapp. There is a service worker (thanks to workbox and webpack) that do the pre-caching of the assets of the app and works good. Now there is a need to add support for the data. The app need to works in offline mode like is online. That means, the user should be able to see al the data and perform a few mutations, that will need to sync back when the app gets online again.

I have the following approaches as possible solutions: * Use a service worker: Add a button that trigger a set of queries to retrieve all the required data for the offline work. Use the service worker (workbox runtimeCaching) to cache all the responses for these queries. When the app goes offline, the service worker will "serve" the response of the queries so the app will work "as if its online", performing the queries as normal with the service worker acting as a "proxy". Set an "optimistic ui" approach for the mutations and use workbox backgroundSync to sync the changes (basically send the mutation action back to the endpoint when the browser get's online again). * Use some apollo native approach (like apollo-cache-persist) to store the queries cache in localStorage. Trigger all the required queries for the app, persist that cache, rehydrate the app from the cache in further loads if the app is not online.

What would be a better and simple approach for an offline web app=

Wreath answered 25/6, 2018 at 20:37 Comment(4)
How did you end? Have you tried apollo-offline?Evin
I finally end using apollo-cache-persist to save the cached data and then rehydrate it when offline. Also add custom query resolvers and custom mutation resolvers so each mutation that is triggered when offline will have response from this resolvers. Then mutation then are stored in a localStorage queue and when a sync is requested I create a sync mutation object that is sent to the serverWreath
Do you have good articles on this topic?Evin
have you looked at github.com/apollographql/apollo-cache-persist ?Holds
S
0

Service-Worker is your best bet. I am currently thinking about this decision, and I have decided to stick with Service-Worker.

Submarine answered 12/1, 2023 at 22:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.