I tore out my hair for hours on this 
Take a very simple example:
const cache = caches.default
const res = new Response('testbody')
cres.headers.append('Cache-Control', 's-maxage=10')
await cache.put('http://test', res)
const cres = await cache.match('http://test')
console.log('Cache match:', cres)
This works as expected, however, if you comment out the cache control header…
Expected: Will cache based on Configure cache by status code · Cloudflare Cache (CDN) docs. I verified the default response does have a status code of 200 in the debugger, so I expect this to work for 2 hours
Actual: Doesn’t cache at all, tested both in miniflare
and with publish
. The cache-control header appears to be mandatory when using the put
api
EDIT: to clarify, I just meant some form of cache headers is mandatory, not explicitly the cache-control header. If you have no cache-related headers, it doesn’t cache, even tho the documentation says it should cache for 120 minutes.
Am I missing something or is this how it works? If so then at best the API should throw an error if this is missing rather than silently not cache but appear to have worked OK, and at worst the documentation needs a big disclaimer so others don’t waste time like I did.
The cache-control header is not mandatory. You can use any of these headers to cache by.
You do need some method of giving a time amount because cache is not permanent storage. If you want something that is permanent, then you want to look at DO, KV or R2.
I think you’re missing my point, did you see my link? It says anything with a 200 status that has no cache headers will be cached for 120 minutes. That’s not happening, it’s not cached at all unless at least one cache header is present. There’s no error, it just silently moves on like it worked, until you try to match and its not there. As soon as you add a cache header, it starts working.
It’s easy to test for yourself per the above, just trying to figure out why the behavior doesn’t match the documentation.
Cache · Cloudflare Workers docs - Cache Control item
Controls caching directives. This is consistent with Cloudflare Cache-Control Directives. Refer to Edge TTL for a list of HTTP response codes and their TTL when Cache-Control directives are not present.
Then edge TTL article says:
Configure cache by status code · Cloudflare Cache (CDN) docs
Sorry, I should have been clearer. With Workers, you are using the Cache API and not edge cache. Workers always run in front of the Edge cache, so you are not able to use it.
Docs about the difference