cloudflarecloudflare-workers

Run Cloudflare Workers while maintaining cached HTML delivery


I'm caching HTML content on Cloudflare and would like to replace client-side pixel tracking with pure server-side tracking - without any client-side initiation. I would like to use Cloudflare Workers to process hits without sacrificing HTML delivery from the cache.

Can Workers (via Routes) be executed "in parallel" to cached hits, or will the cache fulfill the request before the Worker runs?

If the Worker executes first, will a fetch() call to the original origin use the Cloudflare cache, or will it always pull from the server?


Solution

  • Cloudflare Workers run "in front of" cache. Requests go to the Worker first. When the Worker calls fetch(), it fetches through the cache.

    There is also an API you can use to directly read and write the cache from JavaScript:

    https://developers.cloudflare.com/workers/runtime-apis/cache/

    However, you don't need to use this API if all you want is for fetch() to check the cache -- that will happen automatically.