Hit worker only if request have some userAgent

I’m starting with workers and I have configure one, but I only need it to be fired when the userAgent of the request contains some string. I know that I can use a condition in the script, something like …

if (request.headers.get(‘user-agent’).includes(‘customUserAgent’) ){
… my code
}

but I don’t want that the request hits the worker if userAgent doesn’t contain “Googlebot” in order to generate less daily request. Is there any way or workaround to achieve this involving other tools, like firewall or page rules or is there same example of something similar in the workers Template Gallery?

Workers are applied after the CF Firewall, so you can create a rule like this:

And it won’t count as a Worker request.

Well, thank you for the response.
I don’t want to hit the worker, but also I don’t want to block the request. I want that the request has a normal response, but bypassing the worker.

Is there any way?

Firewall rules do offer the ability to skip certain features. Unfortunately, Workers are not among them currently. An idea for a product request over at #feedback:prodreq

1 Like