react-relay-network-modern-ssr icon indicating copy to clipboard operation
react-relay-network-modern-ssr copied to clipboard

Can I populate SSR cache into the QueryResponseCache directly?

Open dehypnosis opened this issue 7 years ago • 2 comments

This works great. Thanks! But I am somewhat worried about that SSR Middleware's cache has no policy like TTL or size which QueryResponseCache has. And I want to unify them. Can I populate cache into relay environment directly?

dehypnosis avatar May 23 '18 07:05 dehypnosis

I just make simple onInit hook to your cacheMiddleware which implements SSR caching like below. Will it cause any unexpected problem?

where create relay network interface

// ... middlewares
cacheMiddleware({
        size: 100, // max 100 requests
        ttl: 900000, // 15 minutes,
        onInit: cacheMap => {
          // rehydrate cache from response script on browser
          if (IS_BROWSER) {
            const cacheArray = window.__API_CACHE__;
            cacheArray.forEach(cacheItem => {
              cacheMap._responses.set(cacheItem[0], cacheItem[1]);
            });
          
          } else { // export extract function for server
            extractCache = () => cacheMap._responses;
          }
        },
      }),
// ...

where serve entry html

...
<script>window.__API_CACHE__=${JSON.stringify(extractCache())};</script>
...

dehypnosis avatar May 23 '18 07:05 dehypnosis

It just should work. I don't see any cases where it will not work or occur errors.

nodkz avatar May 23 '18 08:05 nodkz