Skip to content

Commit 30b4d29

Browse files
committed
Move to Workers
1 parent 080a884 commit 30b4d29

File tree

5 files changed

+31
-9
lines changed

5 files changed

+31
-9
lines changed

.github/workflows/publish-preview.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
runs-on: ubuntu-22.04
1313
permissions:
1414
contents: read
15-
name: Publish to Cloudflare Pages (Preview)
15+
name: Publish Preview
1616
steps:
1717
- uses: actions/checkout@v4
1818
- uses: actions/setup-node@v4
@@ -35,7 +35,7 @@ jobs:
3535
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
3636
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
3737
- run: npm run build:worker && npx wrangler versions upload -c ./wrangler-workers.toml
38-
name: Deploy to Cloudflare Workers [preview]
38+
name: Deploy to Cloudflare Workers
3939
env:
4040
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
4141
- uses: actions/cache/save@v4

.github/workflows/publish-production.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
runs-on: ubuntu-22.04
99
permissions:
1010
contents: read
11-
name: Publish to Cloudflare Pages (Production)
11+
name: Publish Production
1212
steps:
1313
- uses: actions/checkout@v4
1414
- uses: actions/setup-node@v4
@@ -30,6 +30,10 @@ jobs:
3030
env:
3131
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
3232
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
33+
- run: npx wrangler deploy -c ./wrangler-workers.toml
34+
name: Deploy to Cloudflare Workers
35+
env:
36+
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
3337
- uses: actions/cache/save@v4
3438
if: always()
3539
with:

worker/index.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,25 @@ const redirectsEvaluator = generateRedirectsEvaluator(redirectsFileContents);
77

88
export default class extends WorkerEntrypoint<Env> {
99
override async fetch(request: Request) {
10-
const redirect = await redirectsEvaluator(request, this.env.ASSETS);
11-
if (redirect) {
12-
return redirect;
13-
}
10+
try {
11+
// Remove once the whacky double-slash rules get removed
12+
const url = new URL(request.url);
13+
request = new Request(
14+
new URL(
15+
url.pathname + url.search,
16+
"https://developers.cloudflare.com/",
17+
),
18+
request,
19+
);
20+
21+
const redirect = await redirectsEvaluator(request, this.env.ASSETS);
22+
if (redirect) {
23+
return redirect;
24+
}
1425

15-
return await functions.fetch(request, this.env, this.ctx);
26+
return await functions.fetch(request, this.env, this.ctx);
27+
} catch (error) {
28+
return this.env.ASSETS.fetch(request);
29+
}
1630
}
1731
}

wrangler-workers.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
#:schema ./node_modules/wrangler/config-schema.json
12
name = "cloudflare-docs"
23
account_id = "b54f07a6c269ecca2fa60f1ae4920c99"
34
compatibility_date = "2022-09-27"
45
main = "./worker/index.ts"
56

7+
workers_dev = true
8+
route = { pattern = "developers.cloudflare.com/*", zone_name = "developers.cloudflare.com"}
9+
610
rules = [
711
{ type = "Text", globs = ["**/_redirects"], fallthrough = true },
812
]

wrangler.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1+
#:schema ./node_modules/wrangler/config-schema.json
12
name = "cloudflare-docs"
23
compatibility_date = "2022-09-27"
3-

0 commit comments

Comments
 (0)