I’m wondering if I can return a cached response for all requests that don’t contain certain cookies such as
Basically the “Bypass Cache Cookie” page rule which is only available on Business plans.
We recently released a feature (independent of Workers) that does exactly this: https://support.cloudflare.com/hc/en-us/articles/236166048
Unfortunately, Cloudflare Workers does not yet have access to the Cache API – we’re working on it.
Your worker runs before checking cache. More precisely, when you make a subrequest (that is, when you call
fetch()), the subrequest is potentially served from cache. So, ideally, all you’d need to do is mark the subrequest to be non-cacheable. However, at this time, there isn’t a way to do that. We are planning to add one, similar to the other flags described here, but it’s not there yet. Sorry!
Thanks @KentonVarda! What’s the best way of staying up to date with changes such as the one you’re working on?
Purhaps related. I would like to be able to cache the output of a worker. I have a very simple worker that converts all urls to lowercase - do to a legacy system that was case insensitive we have several hundred thousand URLs that are used in all various forms of casing. I have a worker that basically just does
request.url.toLowerCase() and redirects with a 301. I would like the results of these to be catchable so that the worker only runs once per url.
Obviously this would cut my costs down significantly which maybe isn’t what you want, but it would be super helpful to me.
That sounds cool because, from what I understand, that would make workers which make requests even more quicker. Is there an update on when (and if) we can expect this feature?
Edit: Wait, that’s not right what I say. There’s no speed difference with the Worker if the requested asset is already cached at the Cloudflare POP. Do I understand that correctly?
It would be interesting to consult cache, before consulting a worker.
if we use fetch in a worker, and we know that the data fetched is only updated every 30 minutes, there is no sense in running the worker every requests.
Serving the response from cache instead, without running the worker, would:
- make response time much faster
- would not consume our worker resources.
Looking forward to a way to include this.