How to hydrate urql cache from data fetched in Server Components (Next.js App Router)? #3791
-
|
I'm working with the Next.js App Router and trying to reduce duplicate GraphQL requests by hydrating the urql client cache with data fetched inside Server Components. Here's what I understand so far:
However, I’m currently facing the following issue:
Is there any way to inject manually fetched data (from Server Components) into the urql client cache, so that the client doesn’t re-fetch it? If this is currently not supported, would it make sense to expose a utility (e.g., a manual cache write) for this use case? Urql version@urql/core 5.1.1 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Your best bet would be to use the This means that from your server-component you'll need to pass |
Beta Was this translation helpful? Give feedback.
Your best bet would be to use the
restoreDatamethod that is available on the ssrExchange. When you arrive on the client you callssr.restoreData({ [result.operation.key]: result })and that should be enough to have this hydrated in your client cache because the next time this operation goes through the exchange chain the ssr-exchange will reply with the data you restored.This means that from your server-component you'll need to pass
{ [result.operation.key]: result }as props to your first client-component.