use-http icon indicating copy to clipboard operation
use-http copied to clipboard

Post Cache

Open renehauck opened this issue 2 years ago • 8 comments

Describe the bug Hi, I was just about to use this package, but somehow I'm stuck with creating a button that should execute a "post" on a specific route. But the post is only executed once, unless I refresh the page or change a query parameter.

⚠️ Make a Codesandbox ⚠️

// only works once 
const {post} = useFetch(`/hello`)
....
post({id:1})
// Works as expected several times
const {post} = useFetch(`/hello?q=${new Date().getTime()}`)
....
post({id:1})

Am I doing something wrong or is this possibly a bug. Thanks for the work!

renehauck avatar Sep 23 '21 12:09 renehauck

If the body of the post is the same, it will attempt to cache it. Try

const {post} = useFetch(`/hello?q=${new Date().getTime()}`, { cachePolicy: 'no-cache' })

alex-cory avatar Sep 23 '21 20:09 alex-cory

Yes that works, thank you, but should POST/PUT/DELETE be cached by default at all? These methods are supposed to change a state.

renehauck avatar Sep 24 '21 07:09 renehauck

This is great feedback. I agree, I think we should put this into effect. Will get to it as soon as I can.

alex-cory avatar Oct 17 '21 23:10 alex-cory

Would be great if this was documented and really visible. I have spent several hours figuring out what was wrong with my app. And yes, I'm not the most clever developer out there. But still.

gagamil avatar Jun 15 '22 19:06 gagamil

This is still relevant, we ran into the same issue

EdvardasDlugauskas avatar Aug 26 '22 13:08 EdvardasDlugauskas

Interested in this, not caching post, put, delete requests is probably correct but also if you continue to cache get requests (depending on your api) you might still be broken since you wont get the new data you just created... i solved this in a very brute force way by clearing the entire cache after every time I put,post or delete... this makes it so subsequent gets get the new data but also clears out apis that might not have new data... but "might not have" is correct... theres no real way for use-http to understand what your api mightve changed... cache invalidation is difficult lol

kevindurb avatar Feb 02 '23 21:02 kevindurb

I guess if we had access to the cache from inside the response interceptor we could just clear it if request.method !== 'get' to have it centralized

kevindurb avatar Feb 02 '23 21:02 kevindurb

We just ran into this issue as well. I would not expect useFetch to cache POST requests by default; is that a holdover from using use-http with GraphQL?

ottobonn avatar Aug 21 '23 22:08 ottobonn