How to invalidate query made by trpc inside a RSC
In the root page which is a server component, I have fetched some data like this
And I have passed the posts into another component that is a client component, so when we update a
post
from that client component how can we invalidate the posts
in the root page
important notes
- I have used create-t3-app
- in the root page I have used api
imported from @/trpc/server
- in the client component I have used api
imported from @/trpc/react
6 Replies
If you're loading the posts using only
trpc/server
, there is no cache to invalidate. The data is returned as is, server rendered, and never refetched (unless you refresh the page).
If you want to do data invalidation, you'll have to use trpc/react
's useQuery
. And if you want to use the data loaded on server, pass the posts to the client component and use them as initialData
.
So it'd look something like this:
And if you want to invalidate them, you can use trpc/react
's api.useUtils()
hook:
Also, if you're mutating the data and want to update the ones you have in your cache, you might want to optimistically update them instead of refetching. You can do that using utils.posts.list.setData()
.Thank you so much for your help
I have tried router.refresh() and it works fine
I don't know if this approach better than the other one you have mentioned
But it definitly requires much less code to write
Really depends on what you're doing. But advantage of optimistic updates is that you don't need to query the data again. (Unless they could be changed externally or by other users. Then it depends too.)
Also I want to mention that
unstable_cache
supposed to solve the problem but it didn't because in @/trpc/server
we need the headersI'm not sure what you mean, but I don't see how
unstable_cache
is related.unstable_cache
may be similar to use query where you can attach a tag to the callback and you can revalidate it when needed