diff --git a/src/api/client/createCache.ts b/src/api/client/createCache.ts index e9d21a039f..c7a231f0a0 100644 --- a/src/api/client/createCache.ts +++ b/src/api/client/createCache.ts @@ -1,4 +1,5 @@ import { InMemoryCache, PossibleTypesMap, TypePolicies } from '@apollo/client'; +import { Cache } from '@apollo/client/cache'; import { possibleTypes } from '../schema/fragmentMatcher'; import { typePolicies } from '../schema/typePolicies'; @@ -26,5 +27,16 @@ export const createCache = () => { } } + if (typeof BroadcastChannel !== 'undefined') { + const writeChannel = new BroadcastChannel('apollo::write'); + const orig = cache.write.bind(cache); + const ourWrite = (options: Cache.WriteOptions, sendToOthers = true) => { + sendToOthers && writeChannel.postMessage(options); + return orig(options); + }; + writeChannel.onmessage = (event) => ourWrite(event.data, false); + cache.write = ourWrite; + } + return cache; };