Running workers conditionally
Is it possible to run the worker conditionally and not for every incoming request?
In my case, I'd like the worker to run only for document (html) requests, meaning for every request pathname that does not include a period (since having a period in the pathname means directly requesting a file).
2 Replies
I don't think you can achieve this with workers, unfortunately - routes matching doesn't have a way to exclude certain requests, so you could either use a transform rule to change the url (eg. add a prefix for html files that your worker route then matches), or use pages where I think you could use the include/exclude feature in _routes.json to achive this: https://developers.cloudflare.com/pages/functions/routing/#create-a-_routesjson-file
Cloudflare Docs
Routing | Cloudflare Pages docs
Functions utilize file-based routing. Your /functions directory structure determines the designated routes that your Functions will run on. You can create a /functions directory with as many levels as needed for your project’s use case. Review the following directory:
Thanks!