How to cache POST request using Nextjs fetch function ? #56361
Replies: 4 comments 9 replies
-
Mmm that's weird. On Server Components:
If you are making the POST requests from the browser, then there's no caching done by Next.js |
Beta Was this translation helpful? Give feedback.
-
Make sure your POST requests return a 200 status code. Some backend frameworks send a 201 after a POST request, which is not cached: |
Beta Was this translation helpful? Give feedback.
-
Add
|
Beta Was this translation helpful? Give feedback.
-
What I'm wondering and can't seem to find in the documentation is how the cache key from a fetch request is generated. Is it just the url? Does it include the method? Does it include the headers? Does it include the body of the request? Does it account for next.tags? In my case I want to cache some POST requests to an external service. They all share the same url and header (an API key) but differ on their request body. I want them to share the same cache only if their request url, method, headers & body are the same but I'm unsure if just setting a revalidate timer would achieve the expected result. |
Beta Was this translation helpful? Give feedback.
-
Fetch calls made using POST method are not being cached in my app. I want to avoid prop drilling. And multiple components make post request to get some data. I want to use the same cacheing mechanism that "GET" request have in Nextjs fetch function.
Attaching an example, it uses a mock api to make post request to get some random data back as response. The same call is made in Page.tsx and RandomName.tsx but we get different response. Goal is the cache the response in the first call.
eg:
https://stackblitz.com/edit/nextjs-33afry?file=app%2Fpage.tsx
Beta Was this translation helpful? Give feedback.
All reactions