T
TanStack8mo ago
yappiest-sapphire

Handling POST Requests for Data Retrieval & Caching

Hey everyone, All API calls in our system use POST, even for retrieving data. Since POST requests don’t get cached at the network level in the browser (unlike GET), this is causing some inefficiencies. I’m trying to figure out the best way to handle this because: I don’t want to mutate data evertime component load.. The response is stored in Zustand, but I need a way to avoid unnecessary re-fetching. Using useEffect for fetching means the POST request will always run on mount, which isn’t ideal .. i can add a check if there's data on zustand store then no need to fetch .. but that is wrong in my case .. Question: How do you handle caching for POST requests in such cases? Is there a good workaround, Would love to hear any best practices or solutions!
No description
5 Replies
yappiest-sapphire
yappiest-sapphireOP8mo ago
this collection i have in postman.. all post is for retrival .. @TkDodo 🔮 can u kindly guide little bit ..
rival-black
rival-black8mo ago
use react-query ?
yappiest-sapphire
yappiest-sapphireOP8mo ago
Yeah, I’m already using React Query. But React Query treats POST requests as mutations (useMutation), which don't have built-in caching like useQuery. My question is—how can I get caching behavior for a POST request, similar to useQuery?
rival-black
rival-black8mo ago
But React Query treats POST requests as mutations
it does not react query is a promise based, async state manager you can do whatever you want in your queryFn as long as you return a promise and you're aware that it can happen a lot (automatic refetches) if you have an idempotent endpoint that happens to use POST, go for it graphQL is all post requests and also works fine
yappiest-sapphire
yappiest-sapphireOP8mo ago
yeah, got your point! Thanks so much for the clarification! 🙌

Did you find this page helpful?