Skip to content

Commit f5bde66

Browse files
authoredAug 22, 2024··
feat: add router worker to workers-shared (#6537)
* add router worker to workers-shared * address review * add changeset
1 parent e75c581 commit f5bde66

File tree

6 files changed

+51
-2
lines changed

6 files changed

+51
-2
lines changed
 

‎.changeset/empty-laws-prove.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cloudflare/workers-shared": minor
3+
---
4+
5+
feat: add basic Router Worker to workers-shared

‎packages/workers-shared/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ The Asset Server Worker.
88

99
For more details please refer to the dedicated README file.
1010

11+
## `router-worker`
12+
13+
Router Worker.
14+
15+
For more details please refer to the dedicated README file.
16+
1117
> [!NOTE]
1218
> Since code in this package is used by the Workers infrastructure, it is important that PRs are given careful review with regards to how they could cause a failure in production.
1319
> Ideally, there should be comprehensive tests for changes being made to give extra confidence about the behavior.

‎packages/workers-shared/package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@
2222
"dist"
2323
],
2424
"scripts": {
25-
"build": "pnpm run clean && pnpm run bundle:asset-server:prod",
25+
"build": "pnpm run clean && pnpm run bundle:asset-server:prod && pnpm run bundle:router:prod",
2626
"bundle:asset-server": "esbuild asset-server-worker/src/index.ts --format=esm --bundle --outfile=dist/asset-server-worker.mjs --sourcemap=external",
2727
"bundle:asset-server:prod": "pnpm run bundle:asset-server --minify",
28+
"bundle:router": "esbuild router-worker/src/index.ts --format=esm --bundle --outfile=dist/router-worker.mjs --sourcemap=external",
29+
"bundle:router:prod": "pnpm run bundle:router --minify",
2830
"check:lint": "eslint . --max-warnings=0",
2931
"check:type": "tsc",
3032
"clean": "rimraf dist",
31-
"dev": "pnpm run clean && concurrently -n bundle:asset-server -c blue \"pnpm run bundle:asset-server --watch\""
33+
"dev": "pnpm run clean && concurrently -n bundle:asset-server,bundle:router -c blue,magenta \"pnpm run bundle:asset-server --watch\" \"pnpm run bundle:router --watch\""
3234
},
3335
"devDependencies": {
3436
"@cloudflare/eslint-config-worker": "workspace:*",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# `router-worker`
2+
3+
The Router Worker is a [Cloudflare Worker](https://developers.cloudflare.com/workers/) that is responsible for routing between a user worker and static assets in a Workers + Assets project.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type { Fetcher, Request } from "@cloudflare/workers-types";
2+
3+
interface Env {
4+
ASSET_SERVER: Fetcher;
5+
USER_WORKER: Fetcher;
6+
}
7+
export default {
8+
async fetch(request: Request, env: Env) {
9+
const result = await env.ASSET_SERVER.fetch(request);
10+
if (!result.ok) {
11+
if (result.status === 404) {
12+
return await env.USER_WORKER.fetch(request);
13+
}
14+
// return failed response on non-404 errors
15+
return new Response(`Failed to fetch content: ${result.statusText}`, {
16+
status: result.status,
17+
});
18+
}
19+
return result;
20+
},
21+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
##
2+
# Configuration file for the Router Worker
3+
#
4+
# Please note that wrangler has a dependency on this file, and will
5+
# attempt to read it as part of setting up a new Miniflare instance
6+
# in developemnt mode. We should ensure that any configuration changes
7+
# to this file are persisted in wrangler as well, when necessary.
8+
# (see packages/wrangler/src/dev/miniflare.ts -> buildMiniflareOptions())
9+
##
10+
name = "router-worker"
11+
main = "src/index.ts"
12+
compatibility_date = "2024-07-31"

0 commit comments

Comments
 (0)
Please sign in to comment.