Skip to content

Commit

Permalink
Move to Workers
Browse files Browse the repository at this point in the history
  • Loading branch information
GregBrimble committed Oct 14, 2024
1 parent 080a884 commit 815145b
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/publish-preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ubuntu-22.04
permissions:
contents: read
name: Publish to Cloudflare Pages (Preview)
name: Publish Preview
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
Expand All @@ -35,7 +35,7 @@ jobs:
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
- run: npm run build:worker && npx wrangler versions upload -c ./wrangler-workers.toml
name: Deploy to Cloudflare Workers [preview]
name: Deploy to Cloudflare Workers
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
- uses: actions/cache/save@v4
Expand Down
6 changes: 5 additions & 1 deletion .github/workflows/publish-production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
runs-on: ubuntu-22.04
permissions:
contents: read
name: Publish to Cloudflare Pages (Production)
name: Publish Production
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
Expand All @@ -30,6 +30,10 @@ jobs:
env:
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
- run: npx wrangler deploy -c ./wrangler-workers.toml
name: Deploy to Cloudflare Workers
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
- uses: actions/cache/save@v4
if: always()
with:
Expand Down
36 changes: 32 additions & 4 deletions worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,39 @@ const redirectsEvaluator = generateRedirectsEvaluator(redirectsFileContents);

export default class extends WorkerEntrypoint<Env> {
override async fetch(request: Request) {
const redirect = await redirectsEvaluator(request, this.env.ASSETS);
if (redirect) {
return redirect;
try {
try {
// Remove once the whacky double-slash rules get removed
const url = new URL(request.url);
request = new Request(
new URL(
url.pathname.replaceAll("//", "/") + url.search,
"https://developers.cloudflare.com/",
),
request,
);
} catch (error) {
console.error("Could not normalize request URL", error);
}

try {
const redirect = await redirectsEvaluator(request, this.env.ASSETS);
if (redirect) {
return redirect;
}
} catch (error) {
console.error("Could not evaluate redirects", error);
}

try {
return await functions.fetch(request, this.env, this.ctx);
} catch (error) {
console.error("Could not evaluate functions", error);
}
} catch (error) {
console.error("Unknown error", error);
}

return await functions.fetch(request, this.env, this.ctx);
return this.env.ASSETS.fetch(request);
}
}
4 changes: 4 additions & 0 deletions wrangler-workers.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#:schema ./node_modules/wrangler/config-schema.json
name = "cloudflare-docs"
account_id = "b54f07a6c269ecca2fa60f1ae4920c99"
compatibility_date = "2022-09-27"
main = "./worker/index.ts"

workers_dev = true
route = { pattern = "developers.cloudflare.com/*", zone_name = "developers.cloudflare.com"}

rules = [
{ type = "Text", globs = ["**/_redirects"], fallthrough = true },
]
Expand Down
2 changes: 1 addition & 1 deletion wrangler.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#:schema ./node_modules/wrangler/config-schema.json
name = "cloudflare-docs"
compatibility_date = "2022-09-27"

0 comments on commit 815145b

Please sign in to comment.