How to run workers on any subdomain?

Hi there! I have a worker that I'd like to deploy both dev and production versions of to a zone. For simplicities sake, let's say the zone is example.com. For my production worker, I'd like the worker to listen to every request to any subdomain and path to example.com. To do this, I've added the route */* and this works great. However, I'd like to try to scope this a little more so that I can add a dev version to this zone too. What I'd ideally like is for my production worker to handler requests that look like this: *.example.com/*. I'd then like my dev worker to handler requests that look like this: *.dev.example.com/*. Is this possible? Or should I just look into getting a different domain? Thanks!
3 Replies
Cyb3r-Jak3
Cyb3r-Jak32y ago
You can use environments (https://developers.cloudflare.com/workers/platform/environments/) for this
name = "your-worker"
account_id = "your-account-id"

[env.dev]
name = "your-worker-dev"
route = "your-custom-route"
name = "your-worker"
account_id = "your-account-id"

[env.dev]
name = "your-worker-dev"
route = "your-custom-route"
HareBear
HareBearOP2y ago
Thanks @Cyb3r-Jok3 , I probably wasn't clear enough in my original post. After adding the route, I end up with the following message:
The entered route does not have an active DNS record. To add this route, manually add a new DNS record or use the Worker's Custom Domain to create it automatically. Learn more.
The entered route does not have an active DNS record. To add this route, manually add a new DNS record or use the Worker's Custom Domain to create it automatically. Learn more.
For example, adding *.dev.example.com In fact, even just adding *.example.com gives me this error. Actually, I just answered my own question: Adding the following AAAA record seems to have fixed the warning:
*.dev.example.com -> 100::
*.dev.example.com -> 100::
Sorry to bug you!
Cyb3r-Jak3
Cyb3r-Jak32y ago
Yeah DNS records are only created when using custom domains https://developers.cloudflare.com/workers/platform/triggers/custom-domains/. Routes allow you to run workers in front of origins where as custom domains are an origin
Custom Domains · Cloudflare Workers docs
Custom Domains allow you to connect your Worker to a hostname, without having to make changes to your DNS settings or perform any certificate …
Want results from more Discord servers?
Add your server