I was planning to upgrade to nextJS 13 with app router. But I'm not able to make apollo client work with the latest version. Creating a single cache for server components will leads to data leaks as apollo was designed with single user in mind. I found an experimental library(https://github.com/apollographql/apollo-client-nextjs) but I'm not confident enough to use an experimental lib in my project
To prevent data leaks I could create a new in memoryCache if i'm on server every time. But I'm not able to make it work as well.
const getClient = ()=>{
if(!client || typeof window==="undefined"){
client = new ApolloClient({
link:new HttpLink({
uri:"https://graph.staging.strollby.com/graphql",
}),
cache: new InMemoryCache(),
});
}
return client
}
https://github.com/apollographql/apollo-client-nextjs, this library seems the right solution, but unfortunately.
This package is experimental. Generally it should work well, you might run into race conditions when your Client Component is still rendering in SSR, and already making overlapping queries on the browser. This cannot be addressed from our side, but would need API changes in Next.js or React itself. If you do not use suspense in your application, this will not be a problem to you.