question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Merge multiple caches for restoration

See original GitHub issue

We’re looking at micro-frontends with SSR support. Currently each MFE is rendering a part of the page in React, and extracting the data.

Is there a way to extract the InMemoryCache for each MFE, merge them, then create a new single Apollo client on the client using this merged cache?

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
jgan42commented, Jun 15, 2022

As a temporary solution maybe you can try something like this :

const mergeApolloCache = (mainClient: ApolloClient<NormalizedCacheObject>, client: ApolloClient<NormalizedCacheObject>) => {
  const newData = client.cache.extract();

  if (newData) {
    const currentStore: EntityStore = (mainClient.cache as any).data;

    // Extract of cache.replace code without the clear old cache part
    // https://github.com/apollographql/apollo-client/blob/83935e8e1ea2c3eb4a0f10fffbbfb4d51cfc02d2/src/cache/inmemory/entityStore.ts#L335
    const { __META, ...rest } = newData;
    Object.keys(rest).forEach(dataId => {
      currentStore.merge(dataId, rest[dataId] as StoreObject);
    });
    if (__META) {
      __META.extraRootIds.forEach(currentStore.retain, this);
    }
  }
};

I’ve tested a few cases and it seems to work fine.

0reactions
jacobp100commented, Aug 23, 2022

The solution from @jgan42 works well - it’s pretty much using the same code used internally for restoration

Read more comments on GitHub >

github_iconTop Results From Across the Web

multiple cache in a same job (#32814) · Issues - GitLab
Currently in CI pipelines we can use multiple cache using different keys. But we cannot use multiple cache on the same jobs.
Read more >
Advanced topics on caching in Apollo Client
onResetStore multiple times. All of your callbacks are added to an array and are executed concurrently whenever the cache is reset. In this...
Read more >
Caching Strategies - CircleCI
Listing multiple keys for restoring a cache increases the chances of a partial cache hit. However, broadening your restore_cache scope to a wider...
Read more >
All about Using Multiple Cache Pools and Shares in Unraid 6.9
This is the 4th part of a series of videos all about Unraid 6.9. This series will take you through everything that you...
Read more >
Pipeline caching - Azure - Microsoft Learn
This is useful if the pipeline is unable to find an exact match but wants to use a partial cache hit instead. To...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found