Route to a Workers script only when a query string exists?

We have a cost-minded use case where we need to serve a path (without query string) directly from Cloudflare’s CDN cache, and to serve that same path (with query string) from a Workers script.

I haven’t yet found a way to avoid Workers when there’s no query string.

What we need/expect is:

example.com/path  -> <no script>
example.com/path* -> worker-script

In the Cloudflare Workers UI we have Workers disabled on the example.com/path route, and enabled on example.com/path*

But example.com/path still routes to Workers.

I’m aware that the * wildcard operator matches zero or more of any character, but was hoping disabling Workers on example.com/path would take precedence over example.com/path*. It’s arguable, but I feel that’s in the spirit of the Routes doc:

When more than one route pattern could match a request URL, the most specific route pattern wins.

Perhaps someday ?* could be supported to denote a query string.

Given the same path, is there someway to route to a Workers script only when a query string exists?

Workers routes don’t pay attention to Query strings.

1 Like

Thanks for confirming and linking to that prior topic.

Workaround: Since a Page Rules URL pattern can handle query strings, I set a Page Rule to forward example.com/path?* to example.com/path/?$1 with a Workers route of example.com/path/*. This lets example.com/path serve from the CDN.

Letting example.com/path hit the Workers script would have been unnecessarily expensive, to the tune of thousands of U.S. dollars per month. I can see my workaround not working for other sites’ URL structures, so consider this a feature request for query string awareness in Workers routes.

1 Like