twentyhq / twenty

Building a modern alternative to Salesforce, powered by the community.
https://twenty.com
Other
20.27k stars 2.25k forks source link

Change Apollo cache strategy #4914

Open FelixMalfait opened 7 months ago

FelixMalfait commented 7 months ago

As of today we are using "cache-first" strategy and "in-memory" storage.

Instead we would like to introduce a persistent storage through https://github.com/apollographql/apollo-cache-persist/ (most likely LocalStorage).

Instead of using fetchPolicy we will use: initialFetchPolicy: cache-and-network nextFetchPolicy: cache-first

The idea is that the cache coming from localStorage is considered to be less trustworthy (we weren't connected to the app so objects might have changed) - with this setting we ensure that users see cached data immediately for a fast initial load while updating with fresh data from the network. On subsequent load, the data should be trustworthy because we'll have websocket so we can keep the current setting which is cache-first.

I tried to do a quick proof of concept:

FelixMalfait commented 1 day ago

Discussed with Lucas: we will most likely just use cache-and-network even for nextFetchPolicy because the subscription/sync engine will only listen to queries that are in context for the current user (ex: record he's viewing)