Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Post Cache #352

Open
renehauck opened this issue Sep 23, 2021 · 8 comments
Open

Post Cache #352

renehauck opened this issue Sep 23, 2021 · 8 comments

Comments

@renehauck
Copy link

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!

@alex-cory
Copy link
Collaborator

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' })

@renehauck
Copy link
Author

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

@alex-cory
Copy link
Collaborator

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

@gagamil
Copy link

gagamil commented Jun 15, 2022

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.

@EdvardasDlugauskas
Copy link

This is still relevant, we ran into the same issue

@kevindurb
Copy link

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
Copy link

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

@ottobonn
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants