If a worker end point returns cache-control header with public value and other clients hit the same worker end point and receive the cached value, do these cached results still count against the request total for that worker? I sure hope not…
Yes. Workers run before the cache (there’s many reasons to do this and requests are cheap)
Allright, my follow up question is, will the worker function itself be called for cache hits or is it just the request count that would be incremented in those cases?
Yes of course. The request wouldn’t count if the Worker didn’t run
I am attempting to cache the output of my cloudflare worker script. I am returning cache-control header of public,max-age=86400
.
I have created a page rule for my domain to cache everything. Unfortunately I never get a cf-cache-status
of HIT
from cloudflare and every request increments my total worker request count.
What do I need to do to enable caching for my cloudflare worker?
Like Walshy mentioned above earlier, Cloudflare Workers run before cache, so it is not possible to save worker requests the way you are thinking.
You can still make use of Cloudflare’s cache within your Worker to cache requests and return them quickly from Cloudflare’s edge cache, but doing that will still increment your worker request count.
Is there no way to tell a worker to run after cache? If not I’ll look into the cache api you mentioned.
Two MVPs have already said “no”.
Given their level of expertise, I don’t think you’re going to get a different answer the third time around.
You could create a reverse proxy that fetches the content from your worker. However, you’d be effectively killing the whole purpose of workers.