Can workers do this?

We have an endpoint that CloudFlare caches. The thing is that our endpoint does some logging. If a request is cached then we don’t get a true log figure.

My idea being (and not sure if this can be done).

  • Request hits CF worker - detect if CF has it cached, if not just allow the request through
  • If CF has the cache serve that without delay
  • If this was served from the cache ping an HTTPS endpoint asynchronously to do a log action, no response parsed by the workers. Just needs to ping it

Is this sort of thing possible at all?

Shouldnt be an issue.

  • Request comes in
  • You check if it is in the cache
  • If it is not, you simply fetch() it as usual
  • If it is, you ping your logging endpoint with a regular fetch() (plus event.waitUntil() to keep the Worker from being torn down after you return) and serve whatever the cache returned
1 Like

Yep totally possible and is exactly what Logflare does too!

If you’d like to not get charged for the worker requests you can use our Cloudflare app.

If you’d like to just take a look at our source code for the app you can do that too. We’re doing some batching which is a bit fancy but you don’t have to do that. Just make sure you use event.waitUntil.

2 Likes