Skip to content

Commit

Permalink
Deploy to Cloudflare Workers too (cloudflare#16874)
Browse files Browse the repository at this point in the history
* Deploy with Workers Assets too

* Update to only production

* Add preview too

---------

Co-authored-by: kodster28 <[email protected]>
  • Loading branch information
GregBrimble and kodster28 authored Sep 17, 2024
1 parent e9582cc commit f5d70c5
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 5 deletions.
13 changes: 12 additions & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,24 @@ jobs:
env:
NODE_OPTIONS: --max-old-space-size=4096
- run: npx wrangler pages deploy --project-name cloudflare-docs dist
name: Deploy to Cloudflare Pages
env:
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
- run: npm run build:worker && npx wrangler deploy -c ./wrangler-workers.toml
if: github.ref == 'refs/heads/production'
name: Deploy to Cloudflare Workers [production]
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
- run: npm run build:worker && npx wrangler versions upload -c ./wrangler-workers.toml
if: github.ref != 'refs/heads/production'
name: Deploy to Cloudflare Workers [preview]
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
- uses: actions/cache/save@v4
if: always()
with:
path: |
node_modules/.astro/_astro
node_modules/.astro/assets
key: static

3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ pnpm-debug.log*
/playwright/.cache/

.wrangler
/assets/secrets
/assets/secrets
/worker/functions/
9 changes: 8 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "cloudflare-docs-starlight",
"version": "0.0.1",
"private": true,
"type": "module",
"scripts": {
"astro": "astro",
"build": "astro build",
"build:worker": "npx wrangler types -c wrangler-workers.toml --experimental-include-runtime && npx wrangler pages functions build --outdir worker/functions",
"check": "npm run check:functions && npm run check:astro",
"check:astro": "npm run sync && astro check",
"check:functions": "tsc --noEmit -p ./functions/tsconfig.json",
Expand Down Expand Up @@ -47,6 +48,7 @@
"patch-package": "^8.0.0",
"prettier": "^3.3.3",
"prettier-plugin-astro": "^0.14.1",
"redirects-in-workers": "^0.0.5",
"rehype-autolink-headings": "^7.1.0",
"rehype-external-links": "^3.0.0",
"rehype-mermaid": "^2.1.0",
Expand Down
1 change: 1 addition & 0 deletions public/.assetsignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_redirects
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
"~/*": ["src/*"]
}
},
"exclude": ["dist", "functions"]
"exclude": ["dist", "functions", "./worker"]
}
11 changes: 11 additions & 0 deletions worker-configuration.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Generated by Wrangler on Tue Sep 17 2024 00:57:27 GMT-0400 (Eastern Daylight Time)
// by running `wrangler types -c wrangler-workers.toml --experimental-include-runtime`

interface Env {
API_DOCS_KV: KVNamespace;
ASSETS: Fetcher;
}
declare module "*/_redirects" {
const value: string;
export default value;
}
17 changes: 17 additions & 0 deletions worker/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { WorkerEntrypoint } from "cloudflare:workers";
import { generateRedirectsEvaluator } from "redirects-in-workers";
import redirectsFileContents from "../dist/_redirects";
import functions from "./functions";

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;
}

return await functions.fetch(request, this.env, this.ctx);
}
}
19 changes: 19 additions & 0 deletions worker/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"esModuleInterop": true,
"skipLibCheck": true,
"target": "ESNext",
"allowJs": true,
"resolveJsonModule": true,
"moduleDetection": "force",
"isolatedModules": true,
"verbatimModuleSyntax": true,
"strict": true,
"noUncheckedIndexedAccess": true,
"noImplicitOverride": true,
"module": "Preserve",
"noEmit": true,
"lib": ["ESNext"],
"types": ["../.wrangler/types/runtime.d.ts", "../worker-configuration.d.ts"]
}
}
17 changes: 17 additions & 0 deletions wrangler-workers.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name = "cloudflare-docs"
account_id = "b54f07a6c269ecca2fa60f1ae4920c99"
compatibility_date = "2022-09-27"
main = "./worker/index.ts"

rules = [
{ type = "Text", globs = ["**/_redirects"], fallthrough = true },
]

[experimental_assets]
directory = "./dist"
binding = "ASSETS"
not_found_handling = "404-page"

[[kv_namespaces]]
binding = "API_DOCS_KV"
id = "77919060ee0444caa5113f5866b047df"

0 comments on commit f5d70c5

Please sign in to comment.