Worker response is not cached by default?
I have setup workers so that it gets an image from R2 and respond with it. I tried setting Cache-Control: max-age=86400. Response had it, but didn't contain any Cf-Cache-Status. Browser doesn't try to cache it either. Other similar APIs properly caches it with over 10x faster server response time. Do I have to explicitly manage cache in worker code?
btw what is a proper way to set cache headers? using itty-router
5 Replies
You have to manage cache in workers yourself if the data is coming from R2. You can do that with the cache API, but I presume that you've already found that :). As for the browser not caching it, I'm not sure. Do you see the headers you set in the browser?
You can use R2 Custom Domains as well, which work fine with Cache and would cost you less (wouldn't need to pay for worker invocations). If you need custom logic, then you'll have to do cache yourself
https://developers.cloudflare.com/workers/runtime-apis/cache/
You could use Render as well https://github.com/kotx/render, a community library that you can use with Workers and it takes care of cache, range requests, etc
Reading the docs on github, me using *.workers.dev might be the problem? Might be better to just buy new domain
That is correct as well, no cache on workers.dev, only custom domains. That's not why your initial issue happens though, Workers just always run in front of cache
I forgot to set access control allow headers, will play around a bit with custom domains. It was a big hint thanks
Back after fiddling with cache, it's working now. takes about 50ms. But... traditional cf image cdn takes 20ms. Is this the limit of workers? maybe it has fewer cache points than normal cdn? (sorry for replying after long time)