Confused about Caching with Fetch API within Workers

I would like to cache requests made from the Workers Fetch API in the Tiered Cache, which can then be periodically purged globally by URL. This page (https://developers.cloudflare.com/workers/learning/how-the-cache-works/#interacting-with-the-cloudflare-cache) says that all fetches are cached automatically, IF they are proxied through Cloudflare. And the caching can be further customized by operating on the cf object, as in this example https://developers.cloudflare.com/workers/examples/cache-using-fetch/ However, my question is whether it would get cached if I a) do not own the site, and/or b) it is not an active zone on Cloudflare I ask this because in this section (https://developers.cloudflare.com/workers/learning/how-the-cache-works/#single-file-purge--assets-cached-by-a-worker) of the same Workers Cache documentation, it shows a decision tree for cache purging. 1. If the page is not an active CF zone, it can be purged (but how was it cached to begin with?) 2. If it is an active CF zone, but not proxied, it can be purged (again, how was it cached to begin with?) 3. If it is proxied and you own it, you can purge it 4. If you do not own it, it cannot be purged I think I am confused because it isn't clear how a page that isn't active or proxied (1 and 2) can be cached in the first place. Moreover, why can those be purged but #4 can't? Surely I'm missing something fundamental about how this all works. I hope someone can clarify for me!
1 Reply
nickchomey
nickchomey8mo ago
And as a follow-on question: Assuming that is isn't possible to cache and then purge an asset that is proxied through CF but not owned by me, what options do I have available to me to use the Tiered Cache? - Can I somehow replicate this asset into my own zone (be it within Cloudflare or onto my own origin server) for it to be cached from there? - Or if I store the asset in my R2, is there a way to have a tiered cache in front of it? This is precisely how Reserve Caching works, except it is all automatically done via the CDN cache mechanism rather than being done via workers/fetch/R2. I recognize that I can use the Workers Cache API to store the file, but it is not tiered and thus cannot be purged globally. I also recognize that I could use Workers KV (the files are smallish), but the 60 second eventual consistency would cause problems. I much prefer how the Tiered Cache seems to do a global purge in a matter of seconds, and then can be re-"propagated" (via lower-tier request pulls from upper-tier cache/R2 Reserve/Origin). Cache is also free. I found this paragraph further in the same doc that seems to explain the first question: https://developers.cloudflare.com/workers/learning/how-the-cache-works/#fetch
In the context of Workers, a fetch provided by the runtime communicates with the Cloudflare cache. First, fetch checks to see if the URL matches a different zone. If it does, it reads through that zone’s cache (or Worker). Otherwise, it reads through its own zone’s cache, even if the URL is for a non-Cloudflare site. Cache settings on fetch automatically apply caching rules based on your Cloudflare settings. fetch does not allow you to modify or inspect objects before they reach the cache, but does allow you to modify how it will cache.
So, it seems that CF tries its best to use an existing Zone Cache. If one doesn't exist (#1 and 2), then it'll get cached in your zone (and presumably any other zone that is fetching that unproxied page). If a zone does exist, it gets used - if you don't own the zone, then obviously you can't purge it. Its a shame that we can't have the same behaviour of #1 and 2 in #4, but I assume it has to be done this way for the sake of minimizing redundant caching. As for the follow-on question about alternatives, Tiered caching is not possible in front of R2 (despite being how Reserve Caching works). https://developers.cloudflare.com/r2/reference/limits/#caching
Currently Cloudflare’s Tiered Cache feature is not compatible with responses from R2. These responses will act as if Tiered Cache is not configured
Given that I dont want to use Workers Cache API due to no global purging, nor use KV due to eventual consistency issues, my best bet seems to be the first thought: Add some functionality to my origin server that can fetch the 3rd party files and "cache" them there. Then my CF Worker can fetch all of these from a subdomain on my origin server, which will then automatically store it in R2 Reserve Cache -> Smart Upper Tier Cache -> All Lower Tiers (upon request). And I can then purge it all as necessary. I'm already planning to interact with some backend services on the origin server as part of this Worker mechanism, so it shouldn't be too much trouble to add the fetch and store the 3rd party files there as well.