-
Hi :) We're running a fairly large nextjs site with lots of pages and run into the issue of urql cache being a very large payload that is sent to the client since it seems to be sent as a whole. We need SSR content and implemented the solution to provide the extracted ssrCache to the client to be able to use dynamic content like paginations with Urql.
Our client exchanges(graphCache is cacheExchange with some custom resolvers, keys etc.): exchanges: [devtoolsExchange, dedupExchange, graphCache, ssrCache, fetchExchange] getServerSideProps on index page:export async function getServerSideProps() {
const { data: pageData } = await ssrCmsQuery({
query: GetBySlugDocument,
variables: {
slug: '/home',
},
});
return {
props: {
data,
urqlState: ssrCache.extractData(),
},
};
} _app.tsx restoring the cached data for client use:// ...
import { cmsClient, ssrCache, ssrCmsQuery } from '@/lib/urql/client';
function MyApp({ Component, pageProps }) {
pageProps.urqlState && ssrCache.restoreData(pageProps.urqlState);
return (
<AppContext.Provider>
<Provider value={cmsClient}>
<Component {...pageProps} />
</Provider>
</AppContext.Provider>
);
} Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Please please please, do this 😅 If you don't have a mechanism to clear the cache, it'll continue growing indefinitely. The SSR Exchange is purely to replace client-side results with server-side results once. The idea isn't to cache them indefinitely, share the cache between users, and to clear the cache, since often a CDN would be much better suited to caching SSR results. So, our recommendation is to re-create the client for each request If you're sharing the client and results you're also running the risk of leaking authenticated data between requests |
Beta Was this translation helpful? Give feedback.
Please please please, do this 😅 If you don't have a mechanism to clear the cache, it'll continue growing indefinitely. The SSR Exchange is purely to replace client-side results with server-side results once.
The idea isn't to cache them indefinitely, share the cache between users, and to clear the cache, since often a CDN would be much better suited to caching SSR results. So, our recommendation is to re-create the client for each request
If you're sharing the client and results you're also running the risk of leaking authenticated data between requests