Problem with cache API in worker.
I'm having problems with the cache API in my worker.
High-level setup:
I have a single website, let's call it mcguirer.com, with some pages served by a NextJS Pages app (let's say it's at mcguirer.pages.dev), which retrieves page content from a headless CMS.
The mcguirer.com URLs that are served by the pages app, are routed to a worker (mcguirer-worker) that changes the URLs from, for example, https://mcguirer.com/blog/april2025 to https://mcguirer.pages.dev/blog/april2025. The worker then does the fetch() and returns the response. Any URL that is not a worker route for that worker is routed directly to the origin server.
My problem is with getting the pages served by the worker to be cached. I've tried multiple versions of worker code using the cache API, both a the match() and put() idiom and the fetch() idiom, and none of them seem to do that right thing;
I also a cache rule with a trigger condition of http.host eq "mcguirer.com" and an action of "Eligible for cache" and "Ignore cache-control header and use this TTL - 4 hours". However, I'm getting the same worker behavior regardless of whether I have the Cache Rule enabled or not.
BTW, yes, I tried searching for previous messages about this issue. I found a few, but the proposed solutions didn't work. I'm going to continue searching back through the history here.
High-level setup:
I have a single website, let's call it mcguirer.com, with some pages served by a NextJS Pages app (let's say it's at mcguirer.pages.dev), which retrieves page content from a headless CMS.
The mcguirer.com URLs that are served by the pages app, are routed to a worker (mcguirer-worker) that changes the URLs from, for example, https://mcguirer.com/blog/april2025 to https://mcguirer.pages.dev/blog/april2025. The worker then does the fetch() and returns the response. Any URL that is not a worker route for that worker is routed directly to the origin server.
My problem is with getting the pages served by the worker to be cached. I've tried multiple versions of worker code using the cache API, both a the match() and put() idiom and the fetch() idiom, and none of them seem to do that right thing;
- https://developers.cloudflare.com/workers/runtime-apis/cache/
- https://developers.cloudflare.com/workers/examples/cache-using-fetch/
I also a cache rule with a trigger condition of http.host eq "mcguirer.com" and an action of "Eligible for cache" and "Ignore cache-control header and use this TTL - 4 hours". However, I'm getting the same worker behavior regardless of whether I have the Cache Rule enabled or not.
BTW, yes, I tried searching for previous messages about this issue. I found a few, but the proposed solutions didn't work. I'm going to continue searching back through the history here.
Cloudflare Docs
Determine how to cache a resource by setting TTLs, custom cache keys, and cache headers in a fetch request.
