Skip to content

Commit 63b5e8c

Browse files
WC-3583 Disable Sec-Fetch-Mode check when static routing is present
When the Router worker has static routing, the check against "Sec-Fetch-Mode: navigate" is unnecessary. We have explicit static routing to indicate whether or not we should go to a User worker or the Asset worker, and should not try and guess via usually-set browser headers This adds a new parameter to unstable_canFetch RPC method, which should be fine for backwards compatibility, and can be extended in the future if needed. This was necessary because the Asset worker checks the Request headers for Sec-Fetch-Mode to indicate if it can serve an asset (including an index.html or 404.html page based on not_found_handling), but static routing is only provided to the Router worker. Thus, we need to pass more information over RPC
1 parent b242c2e commit 63b5e8c

File tree

4 files changed

+58
-30
lines changed

4 files changed

+58
-30
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export const normalizeConfiguration = (
2020
version: 2,
2121
rules: {},
2222
},
23+
has_static_routing: configuration?.has_static_routing ?? false,
2324
account_id: configuration?.account_id ?? -1,
2425
script_id: configuration?.script_id ?? -1,
2526
debug: configuration?.debug ?? false,

packages/workers-shared/asset-worker/src/handler.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,9 @@ export const canFetch = async (
203203
flagIsEnabled(
204204
configuration,
205205
SEC_FETCH_MODE_NAVIGATE_HEADER_PREFERS_ASSET_SERVING
206-
) && request.headers.get("Sec-Fetch-Mode") === "navigate"
206+
) &&
207+
request.headers.get("Sec-Fetch-Mode") === "navigate" &&
208+
!configuration.has_static_routing
207209
)
208210
) {
209211
configuration = {

packages/workers-shared/asset-worker/tests/handler.test.ts

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,41 +1325,56 @@ describe("[Asset Worker] `canFetch`", () => {
13251325
[{ "Sec-Fetch-Mode": "cors" }, false],
13261326
] as const;
13271327

1328+
const staticRoutingModes = [
1329+
[false, true],
1330+
[true, false],
1331+
] as const;
1332+
13281333
const matrix = [];
13291334
for (const compatibilityOptions of compatibilityOptionsModes) {
13301335
for (const notFoundHandling of notFoundHandlingModes) {
13311336
for (const headers of headersModes) {
1332-
matrix.push({
1333-
compatibilityDate: compatibilityOptions[0].compatibilityDate,
1334-
compatibilityFlags: compatibilityOptions[0].compatibilityFlags,
1335-
notFoundHandling: notFoundHandling[0],
1336-
headers: headers[0],
1337-
expected:
1338-
compatibilityOptions[1] && notFoundHandling[1] && headers[1],
1339-
});
1337+
for (const hasStaticRouting of staticRoutingModes) {
1338+
matrix.push({
1339+
compatibilityDate: compatibilityOptions[0].compatibilityDate,
1340+
compatibilityFlags: compatibilityOptions[0].compatibilityFlags,
1341+
notFoundHandling: notFoundHandling[0],
1342+
headers: headers[0],
1343+
hasStaticRouting: hasStaticRouting[0],
1344+
expected:
1345+
compatibilityOptions[1] &&
1346+
notFoundHandling[1] &&
1347+
headers[1] &&
1348+
hasStaticRouting[1],
1349+
});
1350+
}
13401351
}
13411352
}
13421353
}
13431354

13441355
it.each(matrix)(
1345-
"compatibility_date $compatibilityDate, compatibility_flags $compatibilityFlags, not_found_handling $notFoundHandling, headers: $headers -> $expected",
1356+
"compatibility_date $compatibilityDate, compatibility_flags $compatibilityFlags, not_found_handling $notFoundHandling, headers: $headers, hasStaticRouting $hasStaticRouting -> $expected",
13461357
async ({
13471358
compatibilityDate,
13481359
compatibilityFlags,
13491360
notFoundHandling,
13501361
headers,
1362+
hasStaticRouting,
13511363
expected,
13521364
}) => {
13531365
expect(
13541366
await canFetch(
13551367
new Request("https://example.com/foo", { headers }),
13561368
// @ts-expect-error Empty config default to using mocked jaeger
13571369
mockEnv,
1358-
normalizeConfiguration({
1359-
compatibility_date: compatibilityDate,
1360-
compatibility_flags: compatibilityFlags,
1361-
not_found_handling: notFoundHandling,
1362-
}),
1370+
{
1371+
...normalizeConfiguration({
1372+
compatibility_date: compatibilityDate,
1373+
compatibility_flags: compatibilityFlags,
1374+
not_found_handling: notFoundHandling,
1375+
has_static_routing: hasStaticRouting,
1376+
}),
1377+
},
13631378
exists
13641379
)
13651380
).toBeTruthy();
@@ -1369,11 +1384,14 @@ describe("[Asset Worker] `canFetch`", () => {
13691384
new Request("https://example.com/bar", { headers }),
13701385
// @ts-expect-error Empty config default to using mocked jaeger
13711386
mockEnv,
1372-
normalizeConfiguration({
1373-
compatibility_date: compatibilityDate,
1374-
compatibility_flags: compatibilityFlags,
1375-
not_found_handling: notFoundHandling,
1376-
}),
1387+
{
1388+
...normalizeConfiguration({
1389+
compatibility_date: compatibilityDate,
1390+
compatibility_flags: compatibilityFlags,
1391+
not_found_handling: notFoundHandling,
1392+
has_static_routing: hasStaticRouting,
1393+
}),
1394+
},
13771395
exists
13781396
)
13791397
).toBe(expected);
@@ -1383,11 +1401,14 @@ describe("[Asset Worker] `canFetch`", () => {
13831401
new Request("https://example.com/", { headers }),
13841402
// @ts-expect-error Empty config default to using mocked jaeger
13851403
mockEnv,
1386-
normalizeConfiguration({
1387-
compatibility_date: compatibilityDate,
1388-
compatibility_flags: compatibilityFlags,
1389-
not_found_handling: notFoundHandling,
1390-
}),
1404+
{
1405+
...normalizeConfiguration({
1406+
compatibility_date: compatibilityDate,
1407+
compatibility_flags: compatibilityFlags,
1408+
not_found_handling: notFoundHandling,
1409+
has_static_routing: hasStaticRouting,
1410+
}),
1411+
},
13911412
exists
13921413
)
13931414
).toBeTruthy();
@@ -1397,11 +1418,14 @@ describe("[Asset Worker] `canFetch`", () => {
13971418
new Request("https://example.com/404", { headers }),
13981419
// @ts-expect-error Empty config default to using mocked jaeger
13991420
mockEnv,
1400-
normalizeConfiguration({
1401-
compatibility_date: compatibilityDate,
1402-
compatibility_flags: compatibilityFlags,
1403-
not_found_handling: notFoundHandling,
1404-
}),
1421+
{
1422+
...normalizeConfiguration({
1423+
compatibility_date: compatibilityDate,
1424+
compatibility_flags: compatibilityFlags,
1425+
not_found_handling: notFoundHandling,
1426+
has_static_routing: hasStaticRouting,
1427+
}),
1428+
},
14051429
exists
14061430
)
14071431
).toBeTruthy();

packages/workers-shared/utils/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export const AssetConfigSchema = z.object({
7474
.optional(),
7575
redirects: RedirectsSchema,
7676
headers: HeadersSchema,
77+
has_static_routing: z.boolean().optional(),
7778
...InternalConfigSchema.shape,
7879
});
7980

0 commit comments

Comments
 (0)