-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Closed
Description
I have a question regarding the Cache Hydration in Next.js 13 since they introduced app/ directory + React server components.
What is the purpose of the apolloClient.cache.restore()
and apolloClient.extract()
methods in the context of using Apollo Client with Next.js 13's app directory, and is it necessary to use this to get the existing cache loaded during client-side data fetching and restore the cache with the merged data?
function initializeApollo(initialState: any = null) {
const internalApolloClient = _apolloClient ?? createApolloClient();
// If your page has Next.js data fetching methods that use Apollo Client, the initial state
// get hydrated here
if (initialState) {
// Get existing cache, loaded during client side data fetching
const existingCache = internalApolloClient.extract();
// Merge the existing cache into data passed from getStaticProps/getServerSideProps
const data = merge(initialState, existingCache);
// Restore the cache with the merged data
internalApolloClient.cache.restore(data);
}
// For SSG and SSR always create a new Apollo Client
if (typeof window === 'undefined') return internalApolloClient;
// Create the Apollo Client once in the client
if (!_apolloClient) _apolloClient = internalApolloClient;
return internalApolloClient;
}
Here is a link to the Next.js 13 example:
https://github.com/vercel/next.js/blob/84720697e4f00bd77689153f7cf39421be9b1d25/ex[…]mples/api-routes-apollo-server-and-client-auth/apollo/client.js
karlhorky, detonatR, jorge-armando and ThiLourencokarlhorky, nasatome, jorge-armando, natterstefan and rafaelsmgomes