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
z
zegevlier
05/24/2023, 7:30 PM
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?
c
Chaika
05/24/2023, 7:31 PM
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
i
I love cf
05/24/2023, 7:38 PM
Reading the docs on github, me using *.workers.dev might be the problem? Might be better to just buy new domain
c
Chaika
05/24/2023, 7:39 PM
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
I love cf
05/24/2023, 7:44 PM
I forgot to set access control allow headers, will play around a bit with custom domains. It was a big hint thanks
I love cf
05/25/2023, 6:43 AM
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)