Deploying a worker at multiple routes

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.)

Apparently some users can do this in the Workers UI, but not me.

I’ve been looking for ways to deploy the same worker to multiple routes (in the sense of having more than one route patterns). How did you do this?

1 Like

You simply go into the UI and add more routes, selecting the same script as the one running.

2 Likes

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.

That’s strange. I don’t see a “routes” tab on the editor page.

Because you not in the zone’s Workers tab. You are on the Workers overview editor.

Go into a zone/domain and select workers -> open editor.

1 Like

You can do that with multiple routes.

Is this the page you’re talking about?

Yes. Open the editor.

1 Like

Oh jeez, missed it right in front of my face!

Thanks a lot!

2 Likes

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.

1 Like

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!

1 Like

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

I have not personally used the serverless framework, so I don’t know. Maybe someone else does!

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…

yes it is, read the docs here: Serverless Framework - Cloudflare Workers Guide - Functions

I think I’ll be using the Serverless framework for now, probably until wrangler gets up to par to its support for Workers.

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"

:slightly_smiling_face:

5 Likes

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):

routes = ["example.com/foo/*", "example.com/bar/*"]

Relevant:

Can’t add more than 2 links :frowning:

  • See also, PRs labelled: “subject - routes” github.com/cloudflare/wrangler/issues?q=label%3A%22subject+-+routes%22
  • some discussion about mapping routes to scripts in github.com/cloudflare/wrangler/pull/918#pullrequestreview-323918744