Can you CNAME to a [name].workers.dev worker?

Like many people looking at workers, I would like the Cloudflare cache to be hit first (and so a request only run a worker if it actually needs to, on a cache miss).

Since a worker gets its own domain, like myname.workers.dev, is it possible to set that as an orange cloud DNS record like:

sub.domain.com CNAME myname.workers.dev

… and as such, a request to sub.domain.com does indeed hit the normal Cloudflare cache/proxy first. Good. And then only on a cache miss would it then proceed on to the origin. Which is in this case, a worker. Exactly as I want.

Or would that trick only be possible with an Enterprise plan and its ability to change the Host header (sigh)?

Workers will always on every request on the routing rules.
My main worker approach is Fetch (serve, store in Cache and KV), then on a second request serve from Cache or KV, if not available fetch like the first step.

Under the normal workers logic (ie telling it about the custom domain, like setting it to listen on sub.domain.com/*), yes. As it would intercept that request. What I was wondering was if the worker was not set to use a custom subdomain.

Your fetch approach would indeed use the cache but only after the worker had run. And so that request had incurred a cost.

The main reason you can’t just CNAME to any Cloudflare subdomain (workers.dev or not) is that doing so would require Cloudflare to set up an IP address specifically tailored to handling that worker but with any incoming SNI/HOST header. Without a special IP, the incoming request just looks like any other regular Cloudflare request, since CNAMEs just alias IP addresses and don’t instruct the client to do something like request the CNAME target but instead display the content as the original hostname.

As for hitting the CF cache first, the Workers technical lead himself explained why you can’t just cache in front of the worker to get cheaper Workers pricing:

Thanks.

That KentonVarda post is great. Just been reading that whole discussion.

Yes a CNAME would be the wrong approach to making this work.

To work, it would need an external CDN. In which case … in theory … the name.workers.dev could be its origin. So that way, that CDN would act as the cache layer, and only pass along requests to the worker on a cache miss. Solving the problem. But adding an extra layer of complexity.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.