I’m interested in using Cloudflare to improve page load times, and want to be able to cache html responses in Cloudflare for a long time (e.g. a year) but not allow browsers to cache the page. That way if I change a page I can purge the Cloudflare cache and it’s instantly available to users. Essentially I want to use Cloudflare as an on-the-fly static publishing system.
I was able to achieve this with a header like Cache-Control: s-maxage=86400, max-age=0 but I’ve been reading the spec a bit, and now I’m concerned that the s-maxage property will be picked up by unintended recipients such as ISPs or company proxy caches. If those proxies respect the s-maxage property, then they could continue to hold on to and serve an outdated html response, even after I purge the Cloudflare cache.
I really want to make sure that only Cloudflare will cache my html responses, and no one else (so that I have the ability to instantly purge the cache). Does anyone know how to do this?
On your page rule for caching everything, make sure to include an Edge Cache TTL setting with the desired time to cache the files. This will be the time Cloudflare will keep the cache on its servers. However, given the nature of CF CDN, it will only keep files at datacenters that were previously requested those files. Also, it may discard any cache that is not regularly requested, so do not trust the Edge TTL to keep for a long period of time a page that is infrequently visited.
At the Dashboard > Caching tab, you can set the Browser Cache Expiration to either a TTL you want or to Respect Existing Headers. At a Page Rule, you can only set the Browser Cache TTL to a set amount of time, minimum 30 minutes on the Free Plan.
If you set an specific time for Browser Cache, Cache Cloudflare will still respect existing headers for any amount of time that is greater than the generic Browser Cache Expiration you set.
Thank you for taking the time to help me out @cbrandt and @Judge. Given the limitations and my specific requirements it looks like I’m not quite going to be able to use Cloudflare to cache html in the way I hoped, so I’ll look at other strategies for that part of the equation.