Repository navigation
Clarify Caching Behavior for fetch with POST
#94888
|
The fetch documentation does not clearly explain whether a fetch('https://some-api.com', {
method: 'POST',
body: JSON.stringify({ somePayload }),
cache: 'force-cache',
next: {
tags: ['property'],
},
})From reading the implementation, it appears that a
References: https://github.com/vercel/next.js/blob/v16.3.0-preview.3/packages/next/src/server/lib/patch-fetch.ts#L391-L405 There have been multiple discussions and questions about this topic, but there still doesn't seem to be a clear, definitive answer that users can easily find. It would be helpful if the docs explicitly clarified whether |
Replies: 3 comments 5 replies
|
There are two different behaviors involved here, and that is where most of the confusion comes from:
So for the concrete case people usually mean: await fetch(url, { method: 'POST' })You should treat that as:
unless you explicitly opt into caching behavior. The two relevant doc sections are:
I agree this would be worth stating explicitly in the |
|
Yes - a By default (Next.js 15+), When you pass Conditions that must hold for the
// Cached in the Data Cache, revalidatable by tag
fetch('https://some-api.com', {
method: 'POST',
body: JSON.stringify({ somePayload }),
cache: 'force-cache',
next: { tags: ['property'] },
})One caveat worth noting: with
Hope this fully resolves the question - if it does, would you mind clicking "Mark as answer" so others searching for the same thing can find it quickly? Happy to clarify anything further. |
|
Short answer: yes. A The key thing to keep in mind: caching is opt-in for every request. A
The cache key includes the URL, method, headers, Note With Cache Components, I'll see to add some minimal diff to the fetch docs to cover this.
This is partly because, the initial behavior, cache by default, while the framework specifically opted-out in some cases, which forced you to opt those back into the caching... Teh simpler model as said about, is to opt-in, as you see fit, regardless of method or whatever else. There's some very tiny caveats here and there, but I ain't gonna touch that right now. It also looks like there's a bit of v13/14 code paths that don't work anymore, but when answering LLMs see them, and attempt to build these contrived claims. Even reference |
Short answer: yes. A
POSTrequest is stored in the Data Cache, but only when you explicitly opt in.The key thing to keep in mind: caching is opt-in for every request. A
fetchis written to the Data Cache when all of these hold:cache: 'force-cache', ornext: { revalidate: N }whereN > 0(orfalsefor indefinite), or a route-levelexport const fetchCache/dynamic.200.