Skip to content

Commit ff5717c

Browse files
authored
[Docs Site] Add WorkersTemplates component and use on Quickstarts page (#22098)
* [Docs Site] Add WorkersTemplates component and use on Quickstarts page * small refactor * add token * move button, handle errors and missing package.json
1 parent 69df9e7 commit ff5717c

File tree

6 files changed

+103
-70
lines changed

6 files changed

+103
-70
lines changed

.github/workflows/publish-production.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ jobs:
2727
- run: npm run build
2828
name: Build
2929
env:
30+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3031
NODE_OPTIONS: --max-old-space-size=4096
3132
- run: npx wrangler deploy
3233
name: Deploy to Cloudflare Workers
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
import { AnchorHeading, PackageManagers } from "~/components";
3+
import { fetchWithToken } from "~/util/github";
4+
5+
const REPO = "cloudflare/templates";
6+
7+
const latestCommit = await fetchWithToken(
8+
`https://api.github.com/repos/${REPO}/commits?sha=main&per_page=1`,
9+
)
10+
.then((r) => r.json())
11+
.then((r) => r[0].sha);
12+
13+
const contents = await fetchWithToken(
14+
`https://api.github.com/repos/${REPO}/contents/?ref=${latestCommit}`,
15+
).then((r) => r.json());
16+
17+
const dirs = contents.filter((ent: any) => ent.type === "dir");
18+
---
19+
20+
{
21+
dirs
22+
.filter((dir: any) => dir.name !== ".github")
23+
.map(async (dir: any) => {
24+
const packageJson = await fetch(
25+
`https://gh-code.developers.cloudflare.com/${REPO}/${latestCommit}/${dir.path}/package.json`,
26+
)
27+
.then((r) => r.json())
28+
.catch((reason) => {
29+
console.warn(
30+
`[WorkersTemplates] Failed to parse JSON for ${dir.path}`,
31+
reason,
32+
);
33+
});
34+
35+
if (!packageJson) return;
36+
37+
return (
38+
<div>
39+
<div class="flex items-center justify-between gap-4">
40+
<AnchorHeading depth={3} title={dir.name} />
41+
<a
42+
href={`https://deploy.workers.cloudflare.com/?url=https://github.com/cloudflare/templates/tree/main/${dir.path}`}
43+
>
44+
<img
45+
src="https://deploy.workers.cloudflare.com/button"
46+
alt="Deploy to Cloudflare"
47+
/>
48+
</a>
49+
</div>
50+
<p>{packageJson.description}</p>
51+
<PackageManagers
52+
type="create"
53+
pkg="cloudflare@latest"
54+
args={`--template=cloudflare/templates/${dir.path}`}
55+
/>
56+
</div>
57+
);
58+
})
59+
}

src/components/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export { default as Width } from "./Width.astro";
6565
export { default as WorkersArchitectureDiagram } from "./WorkersArchitectureDiagram.astro";
6666
export { default as WorkersIsolateDiagram } from "./WorkersIsolateDiagram.astro";
6767
export { default as WorkerStarter } from "./WorkerStarter.astro";
68+
export { default as WorkersTemplates } from "./WorkersTemplates.astro";
6869
export { default as YouTube } from "./YouTube.astro";
6970
export { default as YouTubeVideos } from "./YouTubeVideos.astro";
7071

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
title: Workers Templates
3+
styleGuide:
4+
component: WorkersTemplates
5+
---
6+
7+
This component pulls in top-level folders from the [`cloudflare/templates`](https://github.com/cloudflare/templates/tree/main) repository and uses information from the `package.json` (such as `description`) to provide additional information.
8+
9+
```mdx live
10+
import { WorkersTemplates } from "~/components";
11+
12+
<WorkersTemplates />
13+
```

src/content/docs/workers/get-started/quickstarts.mdx

Lines changed: 3 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -9,80 +9,13 @@ description: GitHub repositories that are designed to be a starting point for
99
building a new Cloudflare Workers project.
1010
---
1111

12-
import { LinkButton, WorkerStarter } from "~/components";
12+
import { LinkButton, WorkersTemplates } from "~/components";
1313

1414
Quickstarts are GitHub repositories that are designed to be a starting point for building a new Cloudflare Workers project. To start any of the projects below, run:
1515

16-
```sh
17-
npm create cloudflare@latest <NEW_PROJECT_NAME> -- --template <GITHUB_REPO_URL>
18-
```
16+
## Templates
1917

20-
- `new-project-name`
21-
22-
- A folder with this name will be created with your new project inside, pre-configured to [your Workers account](/workers/wrangler/configuration/).
23-
24-
- `template`
25-
- This is the URL of the GitHub repo starter, as below. Refer to the [create-cloudflare documentation](/pages/get-started/c3/) for a full list of possible values.
26-
27-
## Example Projects
28-
29-
<WorkerStarter
30-
title="Sentry"
31-
repo="mhart/cf-sentry"
32-
description="Log exceptions and errors in your Workers application to Sentry.io - an error tracking tool."
33-
/>
34-
35-
<WorkerStarter
36-
title="Image Color"
37-
repo="xtuc/img-color-worker"
38-
description="Retrieve the dominant color of a PNG or JPEG image."
39-
/>
40-
41-
<WorkerStarter
42-
title="Cloud Storage"
43-
repo="conzorkingkong/cloud-storage"
44-
description="Serve private Amazon Web Services (AWS) bucket files from a Worker script."
45-
/>
46-
47-
<WorkerStarter
48-
title="BinAST"
49-
repo="xtuc/binast-cf-worker-template"
50-
description="Serve a JavaScript Binary AST via a Cloudflare Worker."
51-
/>
52-
53-
<WorkerStarter
54-
title="Edge-Side Rendering"
55-
repo="frandiox/vitessedge-template"
56-
description="Use Vite to render pages on Cloudflare's global network with great DX. Includes i18n, markdown support and more."
57-
/>
58-
59-
---
60-
61-
## Frameworks
62-
63-
<WorkerStarter
64-
title="Apollo GraphQL Server"
65-
repo="cloudflare/workers-graphql-server"
66-
description="Lightning-fast, globally distributed Apollo GraphQL server, deployed on the Cloudflare global network using Cloudflare Workers."
67-
/>
68-
69-
<WorkerStarter
70-
title="GraphQL Yoga"
71-
repo="the-guild-org/yoga-cloudflare-workers-template"
72-
description="The most flexible, fastest, and lightest GraphQL server for all environments, Cloudflare Workers included."
73-
/>
74-
75-
<WorkerStarter
76-
title="Flareact"
77-
repo="flareact/flareact"
78-
description="Flareact is an edge-rendered React framework built for Cloudflare Workers. It features file-based page routing with dynamic page paths and edge-side data fetching APIs."
79-
/>
80-
81-
<WorkerStarter
82-
title="Sunder"
83-
repo="sunderjs/sunder-worker-template"
84-
description="Sunder is a minimal and unopinionated framework for Service Workers. This template uses Sunder, TypeScript, Miniflare, esbuild, Jest, and Sass, as well as Workers Sites for static assets."
85-
/>
18+
<WorkersTemplates />
8619

8720
---
8821

src/util/github.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export async function fetchWithToken(
2+
req: Parameters<typeof fetch>[0],
3+
init?: Parameters<typeof fetch>[1],
4+
) {
5+
if (!import.meta.env.GITHUB_TOKEN) {
6+
const res = await fetch(req);
7+
8+
if (res.status === 403 || res.status === 429) {
9+
throw new Error(
10+
`A request to the GitHub API (${res.url}) was made without a token and was rate limited.\nIf you have the "gh" CLI installed, you can get a token like so: "GITHUB_TOKEN=$(gh auth token) npx astro dev"`,
11+
);
12+
}
13+
14+
return res;
15+
}
16+
17+
const request = new Request(req, init);
18+
19+
return fetch(request, {
20+
...request,
21+
headers: {
22+
...request.headers,
23+
Authorization: `Bearer ${import.meta.env.GITHUB_TOKEN}`,
24+
},
25+
});
26+
}

0 commit comments

Comments
 (0)