react-relay-network-modern-ssr
react-relay-network-modern-ssr copied to clipboard
Can I populate SSR cache into the QueryResponseCache directly?
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?
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>
...
It just should work. I don't see any cases where it will not work or occur errors.