Skip to content

Commit cede77e

Browse files
authored
Add WfP <> Assets docs (#19336)
1 parent 3e917d4 commit cede77e

File tree

3 files changed

+97
-21
lines changed

3 files changed

+97
-21
lines changed
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
diff --git a/node_modules/@astrojs/starlight/user-components/Tabs.astro b/node_modules/@astrojs/starlight/user-components/Tabs.astro
2+
index 4234eb0..276d211 100644
3+
--- a/node_modules/@astrojs/starlight/user-components/Tabs.astro
4+
+++ b/node_modules/@astrojs/starlight/user-components/Tabs.astro
5+
@@ -3,10 +3,11 @@ import Icon from './Icon.astro';
6+
import { processPanels } from './rehype-tabs';
7+
8+
interface Props {
9+
+ IconComponent?: typeof Icon;
10+
syncKey?: string;
11+
}
12+
13+
-const { syncKey } = Astro.props;
14+
+const { syncKey, IconComponent = Icon } = Astro.props;
15+
const panelHtml = await Astro.slots.render('default');
16+
const { html, panels } = processPanels(panelHtml);
17+
18+
@@ -84,7 +85,7 @@ if (isSynced) {
19+
aria-selected={idx === 0 ? 'true' : 'false'}
20+
tabindex={idx !== 0 ? -1 : 0}
21+
>
22+
- {icon && <Icon name={icon} />}
23+
+ {icon && <IconComponent name={icon} />}
24+
{label}
25+
</a>
26+
</li>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
pcx_content_type: navigation
3+
title: Static Assets
4+
external_link: /workers/static-assets/direct-upload/
5+
---

src/content/docs/workers/static-assets/direct-upload.mdx

+66-21
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,18 @@ import {
1616
TabItem,
1717
Tabs,
1818
} from "~/components";
19+
import { Icon } from "astro-icon/components";
1920

2021
:::note
2122

22-
Directly uploading assets via APIs is an advanced approach that most users will not need. Instead, we encourage users to deploy your Worker with [Wrangler](/workers/static-assets/get-started/#1-create-a-new-worker-project-using-the-cli).
23+
Directly uploading assets via APIs is an advanced approach which, unless you are building a programatic integration, most users will not need. Instead, we encourage users to deploy your Worker with [Wrangler](/workers/static-assets/get-started/#1-create-a-new-worker-project-using-the-cli).
2324

2425
:::
2526

2627
Our API empowers users to upload and include static assets as part of a Worker. These static assets can be served for free, and additionally, users can also fetch assets through an optional [assets binding](/workers/static-assets/binding/) to power more advanced applications. This guide will describe the process for attaching assets to your Worker directly with the API.
2728

29+
<Tabs syncKey="workers-vs-platforms" IconComponent={Icon}>
30+
<TabItem icon="workers" label="Workers">
2831
```mermaid
2932
sequenceDiagram
3033
participant User
@@ -33,6 +36,18 @@ sequenceDiagram
3336
User<<->>Workers API: Upload files<br/>POST /client/v4/accounts/:accountId/workers/assets/upload?base64=true
3437
User<<->>Workers API: Upload script version<br/>PUT /client/v4/accounts/:accountId/workers/scripts/:scriptName
3538
```
39+
</TabItem>
40+
<TabItem icon="cloudflare-for-platforms" label="Workers for Platforms" IconComponent={Icon}>
41+
```mermaid
42+
sequenceDiagram
43+
participant User
44+
participant Workers API
45+
User<<->>Workers API: Submit manifest<br/>POST /client/v4/accounts/:accountId/workers/dispatch/namespaces/:dispatchNamespace/scripts/:scriptName/assets-upload-session
46+
User<<->>Workers API: Upload files<br/>POST /client/v4/accounts/:accountId/workers/assets/upload?base64=true
47+
User<<->>Workers API: Upload script version<br/>PUT /client/v4/accounts/:accountId/workers/dispatch/namespaces/:dispatchNamespace/scripts/:scriptName
48+
```
49+
</TabItem>
50+
</Tabs>
3651

3752
The asset upload flow can be distilled into three distinct phases:
3853

@@ -48,6 +63,8 @@ The [manifest upload request](/api/resources/workers/subresources/scripts/subres
4863

4964
`hash` represents a 32 hexadecimal character hash of the file, while `size` is the size (in bytes) of the file.
5065

66+
<Tabs syncKey="workers-vs-platforms" IconComponent={Icon}>
67+
<TabItem icon="workers" label="Workers">
5168
```bash
5269
curl -X POST https://api.cloudflare.com/client/v4/accounts/{account_id}/workers/scripts/{script_name}/assets-upload-session \
5370
--header 'content-type: application/json' \
@@ -69,6 +86,31 @@ curl -X POST https://api.cloudflare.com/client/v4/accounts/{account_id}/workers/
6986
}
7087
}'
7188
```
89+
</TabItem>
90+
<TabItem icon="cloudflare-for-platforms" label="Workers for Platforms">
91+
```bash
92+
curl -X POST https://api.cloudflare.com/client/v4/accounts/{account_id}/workers/dispatch/namespaces/{dispatch_namespace}/scripts/{script_name}/assets-upload-session \
93+
--header 'content-type: application/json' \
94+
--header 'Authorization: Bearer <API_TOKEN>' \
95+
--data '{
96+
"manifest": {
97+
"/filea.html": {
98+
"hash": "08f1dfda4574284ab3c21666d1",
99+
"size": 12
100+
},
101+
"/fileb.html": {
102+
"hash": "4f1c1af44620d531446ceef93f",
103+
"size": 23
104+
},
105+
"/filec.html": {
106+
"hash": "54995e302614e0523757a04ec1",
107+
"size": 23
108+
}
109+
}
110+
}'
111+
```
112+
</TabItem>
113+
</Tabs>
72114

73115
The resulting response will contain a JWT, which provides authentication during file upload. The JWT is valid for one hour.
74116

@@ -110,7 +152,7 @@ Once every file in the manifest has been uploaded, a status code of 201 will be
110152

111153
## Create/Deploy New Version
112154

113-
[Script](/api/resources/workers/subresources/scripts/methods/update/) and [version](/api/resources/workers/subresources/scripts/subresources/versions/methods/create/) upload endpoints require specifying a metadata part in the form data. Here, we can provide the completion token from the previous (upload assets) step.
155+
[Script](/api/resources/workers/subresources/scripts/methods/update/), [Version](/api/resources/workers/subresources/scripts/subresources/versions/methods/create/), and [Workers for Platform script](/api/resources/workers_for_platforms/subresources/dispatch/subresources/namespaces/subresources/scripts/methods/update/) upload endpoints require specifying a metadata part in the form data. Here, we can provide the completion token from the previous (upload assets) step.
114156

115157
```bash title="Example Worker Metadata Specifying Completion Token"
116158
{
@@ -181,6 +223,7 @@ import "node:process";
181223
const accountId: string = ""; // Replace with your actual account ID
182224
const filesDirectory: string = "assets"; // Adjust to your assets directory
183225
const scriptName: string = "my-new-script"; // Replace with desired script name
226+
const dispatchNamespace: string = ""; // Replace with a dispatch namespace if using Workers for Platforms
184227

185228
interface FileMetadata {
186229
hash: string;
@@ -320,16 +363,17 @@ async function scriptUpload(completionToken: string): Promise<void> {
320363
),
321364
);
322365

323-
const response = await fetch(
324-
`https://api.cloudflare.com/client/v4/accounts/${accountId}/workers/scripts/${scriptName}`,
325-
{
326-
method: "PUT",
327-
headers: {
328-
Authorization: `Bearer ${process.env.CLOUDFLARE_API_TOKEN}`,
329-
},
330-
body: form,
366+
const url = dispatchNamespace
367+
? `https://api.cloudflare.com/client/v4/accounts/${accountId}/workers/dispatch/namespaces/${dispatchNamespace}/scripts/${scriptName}`
368+
: `https://api.cloudflare.com/client/v4/accounts/${accountId}/workers/scripts/${scriptName}`;
369+
370+
const response = await fetch(url, {
371+
method: "PUT",
372+
headers: {
373+
Authorization: `Bearer ${process.env.CLOUDFLARE_API_TOKEN}`,
331374
},
332-
);
375+
body: form,
376+
});
333377

334378
if (response.status != 200) {
335379
throw new Error("unexpected status code");
@@ -344,17 +388,18 @@ async function startUploadSession(): Promise<UploadSessionData> {
344388
manifest: fileMetadata,
345389
});
346390

347-
const response = await fetch(
348-
`https://api.cloudflare.com/client/v4/accounts/${accountId}/workers/scripts/${scriptName}/assets-upload-session`,
349-
{
350-
method: "POST",
351-
headers: {
352-
Authorization: `Bearer ${process.env.CLOUDFLARE_API_TOKEN}`,
353-
"Content-Type": "application/json",
354-
},
355-
body: requestBody,
391+
const url = dispatchNamespace
392+
? `https://api.cloudflare.com/client/v4/accounts/${accountId}/workers/dispatch/namespaces/${dispatchNamespace}/scripts/${scriptName}/assets-upload-session`
393+
: `https://api.cloudflare.com/client/v4/accounts/${accountId}/workers/scripts/${scriptName}/assets-upload-session`;
394+
395+
const response = await fetch(url, {
396+
method: "POST",
397+
headers: {
398+
Authorization: `Bearer ${process.env.CLOUDFLARE_API_TOKEN}`,
399+
"Content-Type": "application/json",
356400
},
357-
);
401+
body: requestBody,
402+
});
358403

359404
const data = (await response.json()) as UploadResponse;
360405
const jwt = data.result.jwt;

0 commit comments

Comments
 (0)