use-http
use-http copied to clipboard
Post Cache
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!
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' })
Yes that works, thank you, but should POST/PUT/DELETE be cached by default at all? These methods are supposed to change a state.
This is great feedback. I agree, I think we should put this into effect. Will get to it as soon as I can.
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.
This is still relevant, we ran into the same issue
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
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
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?