From 815145bf185d55a8041d4a2297016b1057eb1728 Mon Sep 17 00:00:00 2001 From: Greg Brimble Date: Mon, 14 Oct 2024 00:40:55 -0400 Subject: [PATCH] Move to Workers --- .github/workflows/publish-preview.yml | 4 +-- .github/workflows/publish-production.yml | 6 +++- worker/index.ts | 36 +++++++++++++++++++++--- wrangler-workers.toml | 4 +++ wrangler.toml | 2 +- 5 files changed, 44 insertions(+), 8 deletions(-) diff --git a/.github/workflows/publish-preview.yml b/.github/workflows/publish-preview.yml index a8820ae842f5c7..8aa9c0b3720a9a 100644 --- a/.github/workflows/publish-preview.yml +++ b/.github/workflows/publish-preview.yml @@ -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 @@ -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 diff --git a/.github/workflows/publish-production.yml b/.github/workflows/publish-production.yml index 7cc74945289f13..df1289496b948f 100644 --- a/.github/workflows/publish-production.yml +++ b/.github/workflows/publish-production.yml @@ -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 @@ -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: diff --git a/worker/index.ts b/worker/index.ts index 9ff4604b5b46af..9e57970a467e05 100644 --- a/worker/index.ts +++ b/worker/index.ts @@ -7,11 +7,39 @@ const redirectsEvaluator = generateRedirectsEvaluator(redirectsFileContents); export default class extends WorkerEntrypoint { 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); } } diff --git a/wrangler-workers.toml b/wrangler-workers.toml index a99cdb3530b78b..aa8c1fcad3af84 100644 --- a/wrangler-workers.toml +++ b/wrangler-workers.toml @@ -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 }, ] diff --git a/wrangler.toml b/wrangler.toml index 10608401534100..a0aaf55ec83248 100644 --- a/wrangler.toml +++ b/wrangler.toml @@ -1,3 +1,3 @@ +#:schema ./node_modules/wrangler/config-schema.json name = "cloudflare-docs" compatibility_date = "2022-09-27" -