You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
After you have created a dispatch namespace, you can fetch any user Workers in the namespaceusing a dynamic dispatch Worker. The dynamic dispatch Worker has a namespace binding.
10
+
A [dynamic dispatch Worker](/cloudflare-for-platforms/workers-for-platforms/reference/how-workers-for-platforms-works/#dynamic-dispatch-worker) is a specialized routing Worker that directs incoming requests to the appropriate user Workers in your dispatch namespace. Instead of using [Workers Routes](/workers/configuration/routing/routes/), dispatch Workers let you programmatically control request routing through code.
10
11
11
-
Use any method of routing to a namespaced Worker (reading the subdomain, request header, or lookup in a database). Ultimately you need the name of the user Worker.
12
12
13
-
In the following example, routing to a user Worker is done through reading the subdomain `<USER_WORKER_NAME>.example.com/*`. For example, `my-customer.example.com` will run the script uploaded to `PUT accounts/<ACCOUNT_ID>/workers/dispatch/namespaces/my-dispatch-namespace/scripts/my-customer`.
13
+

14
+
15
+
16
+
#### Why use a dynamic dispatch Worker?
17
+
18
+
***Scale**: Allows you to route requests to millions of hostnames to different Workers, without defining [Workers Routes](/workers/configuration/routing/routes/) configuration for each one
19
+
***Custom routing logic**: Write code to determine exactly how requests should be routed. For example:
20
+
* Store hostname-to-Worker mappings in [Workers KV](/kv/) and look them up dynamically
21
+
* Route requests based on subdomain, path, headers, or other request properties
22
+
* Use [custom metadata](/cloudflare-for-platforms/cloudflare-for-saas/domain-support/custom-metadata/) attached to [custom hostnames](https://developers.cloudflare.com/cloudflare-for-platforms/cloudflare-for-saas/domain-support/) for routing decisions
23
+
24
+
### Configure the dispatch namespace binding
25
+
To allow your dynamic dispatch Worker to dynamically route requests to Workers in a namespace, you need to configure a dispatch namespace [binding](/workers/runtime-apis/bindings/). This binding enables your dynamic dispatch Worker to call any user Worker within that namespace using `env.dispatcher.get()`.
26
+
27
+
<WranglerConfig>
28
+
29
+
```toml
30
+
[[dispatch_namespaces]]
31
+
binding = "DISPATCHER"
32
+
namespace = "my-dispatch-namespace"
33
+
```
34
+
35
+
</WranglerConfig>
36
+
37
+
Once the binding is configured, your dynamic dispatch Worker can route requests to any Worker in the namespace. Below are common routing patterns you can implement in your dispatcher.
38
+
39
+
### Routing examples
40
+
41
+

42
+
43
+
44
+
#### KV-Based Routing
45
+
Store the routing mappings in [Workers KV](/kv/). This allows you to modify your routing logic without requiring you to change or redeploy the dynamic dispatch Worker.
46
+
47
+
```js
48
+
exportdefault {
49
+
asyncfetch(request, env) {
50
+
try {
51
+
consturl=newURL(request.url);
52
+
53
+
// Use hostname, path, or any combination as the routing key
Route subdomains to the corresponding Worker. For example, `my-customer.example.com` will route to the Worker named `my-customer` in the dispatch namespace.
14
79
15
80
```js
16
81
exportdefault {
17
-
asyncfetch(request, env) {
18
-
try {
19
-
// parse the URL, read the subdomain
20
-
let workerName =newURL(request.url).host.split('.')[0];
21
-
let userWorker =env.dispatcher.get(workerName);
22
-
returnawaituserWorker.fetch(request);
23
-
} catch (e) {
24
-
if (e.message.startsWith('Worker not found')) {
25
-
// we tried to get a worker that doesn't exist in our dispatch namespace
26
-
returnnewResponse('', { status:404 });
27
-
}
28
-
29
-
// this could be any other exception from `fetch()` *or* an exception
30
-
// thrown by the called worker (e.g. if the dispatched worker has
31
-
// `throw MyException()`, you could check for that here).
// User Worker doesn't exist in dispatch namespace
95
+
returnnewResponse('', { status:404 });
96
+
}
97
+
// Could be any other exception from fetch() or from the dispatched Worker
98
+
returnnewResponse(e.message, { status:500 });
99
+
}
100
+
}
35
101
};
36
102
```
103
+
#### Path-Based routing
104
+
Route URL paths to the corresponding Worker. For example, `example.com/customer-1` will route to the Worker named `customer-1` in the dispatch namespace.
0 commit comments