I know the title of this post sounds like a “me-too” but I have read the following documentation, and none of the documentation addressed this issue.
- Respect Existing Headers + no cache headers sent from origin
- Cache headers are ignored
- Tell Cloudflare to Cache Page via Cache Headers
Based on the above information, I set my Cache-Control headers in my response as follows:
HTTP/1.1 200 OK Date: Mon, 12 Mar 2018 21:10:31 GMT Content-Type: application/json Content-Length: 236 Connection: keep-alive **Cache-Control: public, s-maxage=300, max-age=3600** Server: cloudflare CF-RAY: 3fa93f7fdd549a1c-EWR
Our cache-control headers are identifying the response as public, with a CDN cache lifetime of 300 seconds, and a browser/client cache lifetime of an hour.
I was completely unable to get a HIT from cloudflare with this combination.
We opened a support ticket with cloudflare and after a lot of back-and-forth, the engineer said:
Unfortunately, due to the dynamic nature of POST requests it is not possible to cache these requests and see CF-Cache Headers. The Cloudflare system will only cache GET and HEAD requests.
- I do understand that the HTTP spec if somewhat confusing on whether it is valid to cache a POST request (with some saying it is valid, as long as the resource is invalidated), but POST request caching actually is a thing (you can do it in nginx and in varnish for example) and there is no mention in the documentation above that the cache logic only applies to GET requests.
- The engineer’s assertion that POST requests are necessarily more “dynamic” is false. I can create GET requests that are just as “dynamic” as any short POST request.
In any case, I thought I would post this to maybe prevent some other poor soul losing time on this. The documentation does not mention this limitation on any of the pages linked above.
So to sum up: Cloudflare doesn’t support POST caching, so you can’t cache your data API if your API data queries use POST method.
Given the way APIs and microservices are becoming a key part in online architectures, I hope Cloudflare will allow POST caching to be configured. If they are worried about having to calculate hashes on enormous payloads, it could be configured as follows:
Setting: POST cache (ON|OFF)
Setting: POST cache max body parse (1024, 2048, 4096, 16k)
(ie, by setting a limit on the allowable post size for calculating the cache object hash.