Hi there,
What is the recommended way to persist InMemoryCache
when using server side rendering?
If my app was not server side rendered I could just persist to localStorage.
await persistCache({
cache,
storage: window.localStorage,
});
Having trouble trying to figure out how to persist InMemoryCache
when using NextJS.
In my app, I'm using:
import { persistCache } from 'apollo-cache-persist';
import { InMemoryCache } from 'apollo-cache-inmemory';
const initCache = (initialState?: any) => {
const cache = new InMemoryCache().restore(initialState || {});
/**
* Cache uses localStorage to save data.
*
* This cache is used by Apollo (graphql client).
*/
if(typeof window !== 'undefined') {
persistCache({
cache,
storage: window.localStorage
});
}
return cache;
}
export default initCache;
I believe this approach means that SSR won't be able to access the cache but the client will access the cache rather than making network calls once the initial SSR has completed. All calls made from the client will have access to the cache.
@ajhool, thanks for sharing your setup. Can you please show how you are initializing the apollo client?
@ajhool, thanks for sharing your setup. Can you please show how you are initializing the apollo client?
Have you figured you how to implement this? I am running in the same issue?
@ajhool and @sakhmedbayev Woudl you guys be willing to help out and opensource a boilerplate? https://spectrum.chat/apollo/general/best-practice-ac3-persistent-caching-reactive-vars-and-nextjs-ssr~c1f91bc5-e390-46f9-ac70-5e3fb3b23df7
Most helpful comment
In my app, I'm using:
I believe this approach means that SSR won't be able to access the cache but the client will access the cache rather than making network calls once the initial SSR has completed. All calls made from the client will have access to the cache.