Let’s say I have a zone example.com on Cloudflare with three domains, lorem, ipsum, and dolor. How do I deploy a worker to both lorem.example.com and ipsum.example.com (which can’t be matched by a single pattern other than *.example.com/*), but not dolor.example.com? (Using the pattern *.example.com/* will cause all matching domains, including dolor.example.com, to be routed to the worker.)
Hmm… Could you be a bit more specific? I don’t see any option to add/remove routes in the Workers dashboard (https://dash.cloudflare.com/*/workers/overview) or the editor. What’s in your wrangler.toml file?
I have never used Wrangler to deploy multi-route scripts, I don’t know how multiple routes should be configured there. I guess you could easily configure them via the UI and then don’t overwrite them.
For the UI I should have been slightly more specific, you go into each zone’s Workers page, launching the editor there. You’ll see the routes tab.
Did you ever find a solution to deploying to multiple routes in toml file? I want to be able to deploy a worker to multiple routes in our CI pipeline, but currently this doesn’t look to be possible without having multiple toml files or possibly using environments, but they both feel like ugly solutions. Any help much appreciated.
No, but then you don’t really have to. Just configure multiple routes for your Worker in the Dashboard, and choose a principal route to include in your wrangler.toml file. Existing routes won’t be affected when you publish an update, even though Wrangler would only show you the principal route.
Just to confirm, it’s not possible to put more than one route in wrangler.toml today. The team is aware of this limitation, and we’ll see what happens in the future!
Another related question, is it possible to deploy to multiple routes using serverless framework, something like:
functions:
myfunction:
name: myfunction
webpack: true #or the web pack config path for this function
script: handlers/myfunctionhandler
events:
- http:
url: example.com/myfunction
method: GET
- http:
url: example.com/otherfunction
method: GET
Not really sure about the exact structure, but if I remember correctly you can do exactly that. The structure seems correct, but don’t quote me on that.
If I’ll remember to check and I have the files somewhere I should have done it at least once…
Check the 1.8.0-rc.2 release. Wrangler now has multi route support!
name = "worker"
type = "javascript"
account_id = "youraccountid"
# change this line
# route = "example.com/foo/*"
# to this line
routes = ["example.com/foo/*", "example.com/bar/*"]
zone_id = "yourzoneid"
I’ve found this conversation very difficult to follow, but at the time of writing, I think it’s still not possible to map routes to scripts with wrangler as you can with serverless (IIUC: where http “events” route to “functions” (scripts)). There are mentions of wrangler achieving parity with serverless routing in #916, but I believe you still need to do the route:script mapping via the Cloudflare UI.
The following will run src/index.js on all matching routes (drawn from the main property in package.json):