Using APO with Workers?

I was hoping we could call fetch() from within a worker and it’d return the APO-optimized version of the page, but it looks like the worker only receives the requests that APO decides not to handle.

Is there any way to use a worker and APO together?

We have a worker that simply fires off an extra subrequest to a logging service. I can’t work out how to make this work with APO enabled… any advice? Thanks.

Your worker code will be always executed in APO sub-request for non-html requests. On html your worker will be executed only when content haven’t been found in cache, hope it helps.

1 Like

Thanks for the reply - that confirms what I’ve observed.

So is there any way to run a worker on every request? (Ideally even with the ability to change the APO on/off setting from the request.cf object?)

If we used a Cloudflare App, would that run on every request, even those served by APO cache?

If you need to run a worker on every request the only option now is to disable APO. We don’t allow to control APO behavior from user worker code.