relay-tools / react-relay-network-modern-ssr

SSR middleware for react-relay-network-modern
https://github.com/relay-tools/react-relay-network-modern-ssr
MIT License
67 stars 10 forks source link

Can I populate SSR cache into the QueryResponseCache directly? #5

Open dehypnosis opened 6 years ago

dehypnosis commented 6 years ago

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 commented 6 years ago

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>
...
nodkz commented 6 years ago

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