Hi, is it possible to identify within a Worker if an incoming request is for static content that will be eligible for CF caching?
I have a system wherein static resources need to be cached, but also authorised when viewing (User sensitive content)
I’d like to achieve the following in the Worker:
- Is the incoming request for static “cache’able” content?
- If Yes: Authorise via external API, then cache & serve only if authorised
- If No: Forward the request on as normal to the application
Essentially, I don’t want add or get anything from the CF cache unless it has been authorised. But I don’t want the overhead of authorising all requests, including dynamic ones that will never be cached
Failing this - Is there some other way of doing authorisation + caching on static content?
Cloudflare uses the response headers & file extension to determine this - https://developers.cloudflare.com/cache/about/default-cache-behavior/
There’s nothing built in to Workers to tell you that, you’d need to do your own checks within the Worker based on the URL/Response.
Thanks for the response & link
Makes sense that CF uses the response headers and file extension. Unfortunately I need to establish this check well before I have a Response, in Step 2 here:
1 Request >> 2 Worker >> 3 Authorise (If cach’able resource) >> 4 Response
So it sounds like the only option is to inspect the request URL file extension and see if it matches one of the 56 Default cached file extensions
I’m a little surprised there isn’t an easier way of doing this as there are obvious scenarios with a requirement for caching, but only with authorisation. Any service that has authorised users with private assets would need this. I.e. UserA should not be able to access the cached assets of UserB
This is very specifically a Workers scenario since the ‘usual’ Cloudflare cache would run before any logic on your origin if the asset was in the cache, foregoing any sort of authorisation that you’d be hoping to do whereas Workers are before the cache.