Skip to content

Commit 96c27f9

Browse files
WC-3583 Update static routing for run_worker_first based configuration
Now that we're not providing this configuration via _routes.json, we can relax some of the configuration. Much of this happens on the backend, but some field names have been changed ("include" -> "worker", "exclude"->"asset")
1 parent 04fb858 commit 96c27f9

File tree

6 files changed

+21
-29
lines changed

6 files changed

+21
-29
lines changed

.changeset/tasty-hoops-own.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
"@cloudflare/workers-shared": minor
33
---
44

5-
Adds support for static routing (i.e. \_routes.json) to Workers Assets
5+
Adds support for static routing to Workers Assets
66

77
Implements the proposal noted here https://github.com/cloudflare/workers-sdk/discussions/9143
88

9-
In brief: when static routing is present for a Worker with assets, routing via those static rules takes precedence. When a request is evaluated in the Router Worker, the request path is first compared to the "exclude" rules. If any match, the request is forwarded directly to the Asset Worker. If instead any "include" rules match, the request is forwarded directly to the User Worker. If neither match (or static routing was not provided), the existing behavior takes over.
9+
In brief: when static routing is present for a Worker with assets, routing via those static rules takes precedence. When a request is evaluated in the Router Worker, the request path is first compared to the "asset" rules (which are to be specific via "negative" rules, e.g. "!/api/assets"). If any match, the request is forwarded directly to the Asset Worker. If instead any "user_worker" rules match, the request is forwarded directly to the User Worker. If neither match (or static routing was not provided), the existing behavior takes over.
1010

11-
As part of this explicit routing, when static routing is present, the check against "Sec-Fetch-Mode: navigate" (to guess if this should serve an asset or go to the User Worker for not_found_handling) is disabled. Routing can be controlled by uploading a \_routes.json, and asset serving (including when an index.html or 404.html page is served) will be more simple.
11+
As part of this explicit routing, when static routing is present, the check against "Sec-Fetch-Mode: navigate" (to guess if this should serve an asset or go to the User Worker for not_found_handling) is disabled. Routing can be controlled by setting routing rules via "run_worker_first", and asset serving (including when an index.html or 404.html page is served) will be more simple.

packages/workers-shared/router-worker/src/analytics.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ const VERSION = 1;
66
export enum STATIC_ROUTING_DECISION {
77
NOT_PROVIDED = 0,
88
NOT_ROUTED = 1,
9-
EXCLUDE = 2,
10-
INCLUDE = 3,
9+
ROUTED = 2,
1110
}
1211

1312
export enum DISPATCH_TYPE {

packages/workers-shared/router-worker/src/configuration.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ export const applyConfigurationDefaults = (
1111
script_id: configuration?.script_id ?? -1,
1212
debug: configuration?.debug ?? false,
1313
static_routing: configuration?.static_routing ?? {
14-
version: 1,
15-
include: [],
14+
worker: [],
1615
},
1716
};
1817
};

packages/workers-shared/router-worker/src/worker.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export default {
8080
if (config.static_routing) {
8181
// evaluate "exclude" rules
8282
const excludeRulesMatcher = generateStaticRoutingRuleMatcher(
83-
config.static_routing.exclude ?? []
83+
config.static_routing.asset ?? []
8484
);
8585
if (
8686
excludeRulesMatcher({
@@ -90,7 +90,7 @@ export default {
9090
// direct to asset worker
9191
analytics.setData({
9292
dispatchtype: DISPATCH_TYPE.ASSETS,
93-
staticRoutingDecision: STATIC_ROUTING_DECISION.EXCLUDE,
93+
staticRoutingDecision: STATIC_ROUTING_DECISION.ROUTED,
9494
});
9595
return await env.JAEGER.enterSpan("dispatch_assets", async (span) => {
9696
span.setTags({
@@ -104,7 +104,7 @@ export default {
104104
}
105105
// evaluate "include" rules
106106
const includeRulesMatcher = generateStaticRoutingRuleMatcher(
107-
config.static_routing.include
107+
config.static_routing.worker
108108
);
109109
if (
110110
includeRulesMatcher({
@@ -119,7 +119,7 @@ export default {
119119
// direct to user worker
120120
analytics.setData({
121121
dispatchtype: DISPATCH_TYPE.WORKER,
122-
staticRoutingDecision: STATIC_ROUTING_DECISION.INCLUDE,
122+
staticRoutingDecision: STATIC_ROUTING_DECISION.ROUTED,
123123
});
124124
return await env.JAEGER.enterSpan("dispatch_worker", async (span) => {
125125
span.setTags({

packages/workers-shared/router-worker/tests/index.test.ts

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,15 @@ describe("unit tests", async () => {
9595
expect(await response.text()).toEqual("hello from asset worker");
9696
});
9797

98-
it("it returns fetch from user worker when static_routing include rule matches", async () => {
98+
it("it returns fetch from user worker when static_routing worker rule matches", async () => {
9999
const request = new Request("https://example.com/api/includeme");
100100
const ctx = createExecutionContext();
101101

102102
const env = {
103103
CONFIG: {
104104
has_user_worker: true,
105105
static_routing: {
106-
version: 1,
107-
include: ["/api/*"],
106+
worker: ["/api/*"],
108107
},
109108
},
110109
USER_WORKER: {
@@ -126,17 +125,16 @@ describe("unit tests", async () => {
126125
expect(await response.text()).toEqual("hello from user worker");
127126
});
128127

129-
it("it returns fetch from asset worker when static_routing exclude rule matches", async () => {
128+
it("it returns fetch from asset worker when static_routing asset rule matches", async () => {
130129
const request = new Request("https://example.com/api/excludeme");
131130
const ctx = createExecutionContext();
132131

133132
const env = {
134133
CONFIG: {
135134
has_user_worker: true,
136135
static_routing: {
137-
version: 1,
138-
include: ["/api/includeme"],
139-
exclude: ["/api/excludeme"],
136+
worker: ["/api/includeme"],
137+
asset: ["/api/excludeme"],
140138
},
141139
},
142140
USER_WORKER: {
@@ -158,17 +156,16 @@ describe("unit tests", async () => {
158156
expect(await response.text()).toEqual("hello from asset worker");
159157
});
160158

161-
it("it returns fetch from asset worker when static_routing exclude and include rule matches", async () => {
159+
it("it returns fetch from asset worker when static_routing asset and worker rule matches", async () => {
162160
const request = new Request("https://example.com/api/excludeme");
163161
const ctx = createExecutionContext();
164162

165163
const env = {
166164
CONFIG: {
167165
has_user_worker: true,
168166
static_routing: {
169-
version: 1,
170-
include: ["/api/*"],
171-
exclude: ["/api/excludeme"],
167+
worker: ["/api/*"],
168+
asset: ["/api/excludeme"],
172169
},
173170
},
174171
USER_WORKER: {
@@ -198,8 +195,7 @@ describe("unit tests", async () => {
198195
CONFIG: {
199196
has_user_worker: true,
200197
static_routing: {
201-
version: 1,
202-
include: ["/api/*"],
198+
worker: ["/api/*"],
203199
},
204200
},
205201
USER_WORKER: {
@@ -229,8 +225,7 @@ describe("unit tests", async () => {
229225
CONFIG: {
230226
has_user_worker: true,
231227
static_routing: {
232-
version: 1,
233-
include: ["/api/*"],
228+
worker: ["/api/*"],
234229
},
235230
},
236231
USER_WORKER: {

packages/workers-shared/utils/types.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ const InternalConfigSchema = z.object({
77
});
88

99
const StaticRoutingSchema = z.object({
10-
version: z.literal(1),
11-
include: z.array(z.string()),
12-
exclude: z.array(z.string()).optional(),
10+
worker: z.array(z.string()),
11+
asset: z.array(z.string()).optional(),
1312
});
1413

1514
export const RouterConfigSchema = z.object({

0 commit comments

Comments
 (0)