Can Cloudflare cache html response from worker?

Dear members, I really appreciate your time to look at my problem, I’m new to workers. I created a worker to return a string as HTML. Can Cloudflare cache html response from worker? I did not see it cache the HTML response I have tried to add a response header “Cache-Control” “ export default { async fetch(request, env, ctx) { const url = new URL(request.url); const html = <!DOCTYPE html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <h1> ${url} ------> ${new Date().toLocaleString()}"</h1> <p>This markup was generated by a Cloudflare Worker.</p> </body>; return new Response(html, { headers: { "content-type": "text/html;charset=UTF-8", "Cache-Control": "public, max-age=300, s-maxage=600" }, }); } }; “ I also tried to create a cache rule for cache everything. I still did not see a catch header available in response. My worker URLs: https://whatfood-sow-worker.whatfood.workers.dev/ https://production.whatfood-sow-worker.whatfood.workers.dev/ https://sow.whatfood.work/ How to achieve cache HTML response?
6 Replies
Chaika
Chaika16mo ago
Workers can use the Cache API to cache things, but they themselves cannot be cached. The Cache API will also only work on custom domains: https://developers.cloudflare.com/workers/examples/cache-api/ https://developers.cloudflare.com/workers/runtime-apis/cache/
Using the Cache API · Cloudflare Workers docs
Documentation for Cloudflare Workers, a serverless execution environment that allows you to create entirely new applications or augment existing ones …
Cache · Cloudflare Workers docs
The Cache API allows fine grained control of reading and writing from the Cloudflare global network cache.
Chaika
Chaika16mo ago
The Cache API is also only local cloudflare colo (location) cache, and does not support Tiered Caching or Cache Reserve, and of course your worker will always be invocated, even just to check and return a response from cache
node java
node javaOP16mo ago
Thanks man, I thought cloudflare will default cache it . thank you very much.
Chaika
Chaika16mo ago
Sure, I replied to your community post with an updated example to include caching Note: There's not really any advantage here, as the worker will run anyway, it's probably faster to not use cache, but if you're just learning it's a good example.
node java
node javaOP16mo ago
Thank you so much and your time. I just started it yesterday. I am very impressed with the speed of the worker.
starina_cool
starina_cool14mo ago
But what about this quote from docs: "Cloudflare Workers can run before and after the cache but can also be utilized to modify assets once they are returned from the cache. " https://developers.cloudflare.com/workers/learning/how-the-cache-works/
How the Cache works · Cloudflare Workers docs
How Workers interacts with the Cloudflare cache.
Want results from more Discord servers?
Add your server