Skip to content

Commit f5d70c5

Browse files
Deploy to Cloudflare Workers too (#16874)
* Deploy with Workers Assets too * Update to only production * Add preview too --------- Co-authored-by: kodster28 <[email protected]>
1 parent e9582cc commit f5d70c5

File tree

10 files changed

+91
-5
lines changed

10 files changed

+91
-5
lines changed

.github/workflows/publish.yml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,24 @@ jobs:
2727
env:
2828
NODE_OPTIONS: --max-old-space-size=4096
2929
- run: npx wrangler pages deploy --project-name cloudflare-docs dist
30+
name: Deploy to Cloudflare Pages
3031
env:
3132
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
3233
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
34+
- run: npm run build:worker && npx wrangler deploy -c ./wrangler-workers.toml
35+
if: github.ref == 'refs/heads/production'
36+
name: Deploy to Cloudflare Workers [production]
37+
env:
38+
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
39+
- run: npm run build:worker && npx wrangler versions upload -c ./wrangler-workers.toml
40+
if: github.ref != 'refs/heads/production'
41+
name: Deploy to Cloudflare Workers [preview]
42+
env:
43+
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
3344
- uses: actions/cache/save@v4
45+
if: always()
3446
with:
3547
path: |
3648
node_modules/.astro/_astro
3749
node_modules/.astro/assets
3850
key: static
39-

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ pnpm-debug.log*
2424
/playwright/.cache/
2525

2626
.wrangler
27-
/assets/secrets
27+
/assets/secrets
28+
/worker/functions/

package-lock.json

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{
22
"name": "cloudflare-docs-starlight",
3-
"version": "0.0.1",
3+
"private": true,
44
"type": "module",
55
"scripts": {
66
"astro": "astro",
77
"build": "astro build",
8+
"build:worker": "npx wrangler types -c wrangler-workers.toml --experimental-include-runtime && npx wrangler pages functions build --outdir worker/functions",
89
"check": "npm run check:functions && npm run check:astro",
910
"check:astro": "npm run sync && astro check",
1011
"check:functions": "tsc --noEmit -p ./functions/tsconfig.json",
@@ -47,6 +48,7 @@
4748
"patch-package": "^8.0.0",
4849
"prettier": "^3.3.3",
4950
"prettier-plugin-astro": "^0.14.1",
51+
"redirects-in-workers": "^0.0.5",
5052
"rehype-autolink-headings": "^7.1.0",
5153
"rehype-external-links": "^3.0.0",
5254
"rehype-mermaid": "^2.1.0",

public/.assetsignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
_redirects

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
"~/*": ["src/*"]
77
}
88
},
9-
"exclude": ["dist", "functions"]
9+
"exclude": ["dist", "functions", "./worker"]
1010
}

worker-configuration.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Generated by Wrangler on Tue Sep 17 2024 00:57:27 GMT-0400 (Eastern Daylight Time)
2+
// by running `wrangler types -c wrangler-workers.toml --experimental-include-runtime`
3+
4+
interface Env {
5+
API_DOCS_KV: KVNamespace;
6+
ASSETS: Fetcher;
7+
}
8+
declare module "*/_redirects" {
9+
const value: string;
10+
export default value;
11+
}

worker/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { WorkerEntrypoint } from "cloudflare:workers";
2+
import { generateRedirectsEvaluator } from "redirects-in-workers";
3+
import redirectsFileContents from "../dist/_redirects";
4+
import functions from "./functions";
5+
6+
const redirectsEvaluator = generateRedirectsEvaluator(redirectsFileContents);
7+
8+
export default class extends WorkerEntrypoint<Env> {
9+
override async fetch(request: Request) {
10+
const redirect = await redirectsEvaluator(request, this.env.ASSETS);
11+
if (redirect) {
12+
return redirect;
13+
}
14+
15+
return await functions.fetch(request, this.env, this.ctx);
16+
}
17+
}

worker/tsconfig.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"compilerOptions": {
3+
"esModuleInterop": true,
4+
"skipLibCheck": true,
5+
"target": "ESNext",
6+
"allowJs": true,
7+
"resolveJsonModule": true,
8+
"moduleDetection": "force",
9+
"isolatedModules": true,
10+
"verbatimModuleSyntax": true,
11+
"strict": true,
12+
"noUncheckedIndexedAccess": true,
13+
"noImplicitOverride": true,
14+
"module": "Preserve",
15+
"noEmit": true,
16+
"lib": ["ESNext"],
17+
"types": ["../.wrangler/types/runtime.d.ts", "../worker-configuration.d.ts"]
18+
}
19+
}

wrangler-workers.toml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name = "cloudflare-docs"
2+
account_id = "b54f07a6c269ecca2fa60f1ae4920c99"
3+
compatibility_date = "2022-09-27"
4+
main = "./worker/index.ts"
5+
6+
rules = [
7+
{ type = "Text", globs = ["**/_redirects"], fallthrough = true },
8+
]
9+
10+
[experimental_assets]
11+
directory = "./dist"
12+
binding = "ASSETS"
13+
not_found_handling = "404-page"
14+
15+
[[kv_namespaces]]
16+
binding = "API_DOCS_KV"
17+
id = "77919060ee0444caa5113f5866b047df"

0 commit comments

Comments
 (0)