Skip to content

Commit 1672a33

Browse files
dinasaur404hyperlint-ai[bot]RebeccaTamachiro
authored
Workers for Platforms: Static assets docs (cloudflare#19527)
* docs: add static assets documentation for Workers for Platforms * Update src/content/docs/cloudflare-for-platforms/workers-for-platforms/configuration/static-assets.mdx Co-authored-by: hyperlint-ai[bot] <154288675+hyperlint-ai[bot]@users.noreply.github.com> * Update src/content/docs/cloudflare-for-platforms/workers-for-platforms/configuration/static-assets.mdx Co-authored-by: hyperlint-ai[bot] <154288675+hyperlint-ai[bot]@users.noreply.github.com> * Update src/content/docs/cloudflare-for-platforms/workers-for-platforms/configuration/static-assets.mdx Co-authored-by: hyperlint-ai[bot] <154288675+hyperlint-ai[bot]@users.noreply.github.com> * Update src/content/docs/cloudflare-for-platforms/workers-for-platforms/configuration/static-assets.mdx Co-authored-by: hyperlint-ai[bot] <154288675+hyperlint-ai[bot]@users.noreply.github.com> * Fix heading levels and other formatting * Style guide changes and replace CF's edge for global network * Add meta description and replace bold by h4 --------- Co-authored-by: hyperlint-ai[bot] <154288675+hyperlint-ai[bot]@users.noreply.github.com> Co-authored-by: Rebecca Tamachiro <[email protected]>
1 parent d2dbe0d commit 1672a33

File tree

1 file changed

+255
-3
lines changed
  • src/content/docs/cloudflare-for-platforms/workers-for-platforms/configuration

1 file changed

+255
-3
lines changed
Lines changed: 255 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,257 @@
11
---
2-
pcx_content_type: navigation
3-
title: Static Assets
4-
external_link: /workers/static-assets/direct-upload/
2+
pcx_content_type: concept
3+
title: Static assets
4+
description: Host static assets on Cloudflare's global network and deliver faster load times worldwide with Workers for Platforms.
55
---
6+
Workers for Platforms lets you deploy front-end applications at scale. By hosting static assets on Cloudflare's global network, you can deliver faster load times worldwide and eliminate the need for external infrastructure. You can also combine these static assets with dynamic logic in Cloudflare Workers, providing a full-stack experience for your customers.
7+
8+
### What you can build
9+
10+
#### Static sites
11+
Host and serve HTML, CSS, JavaScript, and media files directly from Cloudflare's network, ensuring fast loading times worldwide. This is ideal for blogs, landing pages, and documentation sites.
12+
13+
#### Full-stack applications
14+
Combine asset hosting with Cloudflare Workers to power dynamic, interactive applications. Store and retrieve data using Cloudflare KV, D1, and R2 Storage, allowing you to serve both front-end assets and backend logic from a single Worker.
15+
16+
### Benefits
17+
18+
#### Global caching for faster performance
19+
Cloudflare automatically caches static assets at data centers worldwide, reducing latency and improving load times by up to 2x for users everywhere.
20+
21+
#### Scalability without infrastructure management
22+
Your applications scale automatically to handle high traffic without requiring you to provision or manage infrastructure. Cloudflare dynamically adjusts to demand in real time.
23+
24+
#### Unified deployment for static and dynamic content
25+
Deploy front-end assets alongside server-side logic, all within Cloudflare Workers. This eliminates the need for a separate hosting provider and ensures a streamlined deployment process.
26+
27+
---
28+
29+
## Deploy static assets to User Workers
30+
31+
It is common that, as the Platform, you will be responsible for uploading static assets on behalf of your end users. This often looks like this:
32+
33+
1. Your user uploads files (HTML, CSS, images) through your interface.
34+
2. Your platform interacts with the Workers for Platforms APIs to attach the static assets to the User Worker script.
35+
36+
Once you receive the static files from your users (for a new or updated site), complete the following steps to attach the files to the corresponding User Worker:
37+
38+
1. Create an Upload Session
39+
2. Upload file contents
40+
3. Deploy/Update the Worker
41+
42+
After these steps are completed, the User Worker's static assets will be live on the Cloudflare's global network.
43+
44+
### 1. Create an Upload Session
45+
46+
Before sending any file data, you need to tell Cloudflare which files you intend to upload. That list of files is called a manifest. Each item in the manifest includes:
47+
48+
* A file path (for example, `"/index.html"` or `"/assets/logo.png"`)
49+
* A hash (32-hex characters) representing the file contents
50+
* The file size in bytes
51+
52+
#### Example manifest (JSON)
53+
54+
```json
55+
{
56+
"/index.html": {
57+
"hash": "08f1dfda4574284ab3c21666d1ee8c7d4",
58+
"size": 1234
59+
},
60+
"/styles.css": {
61+
"hash": "36b8be012ee77df5f269b11b975611d3",
62+
"size": 5678
63+
}
64+
}
65+
```
66+
67+
To start the upload process, send a POST request to the Create Assets Upload Session [API endpoint](/api/resources/workers_for_platforms/subresources/dispatch/subresources/namespaces/subresources/scripts/subresources/asset_upload/methods/create/).
68+
69+
```bash
70+
POST /accounts/{account_id}/workers/dispatch/namespaces/{namespace}/scripts/{script_name}/assets-upload-session
71+
```
72+
73+
Path Parameters:
74+
* `namespace`: Name of the Workers for Platforms dispatch namespace
75+
* `script_name`: Name of the User Worker
76+
77+
In the request body, include a JSON object listing each file path along with its hash and size. This helps Cloudflare identify which files you intend to upload and allows Cloudflare to check if any of them are already stored.
78+
79+
#### Sample request
80+
81+
```bash
82+
curl -X POST \
83+
"https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/workers/dispatch/namespaces/$NAMESPACE_NAME/scripts/$SCRIPT_NAME/assets-upload-session" \
84+
-H "Content-Type: application/json" \
85+
-H "Authorization: Bearer $API_TOKEN" \
86+
--data '{
87+
"manifest": {
88+
"/index.html": {
89+
"hash": "08f1dfda4574284ab3c21666d1ee8c7d4",
90+
"size": 1234
91+
},
92+
"/styles.css": {
93+
"hash": "36b8be012ee77df5f269b11b975611d3",
94+
"size": 5678
95+
}
96+
}
97+
}'
98+
```
99+
100+
#### Generating the hash
101+
102+
You can compute a SHA-256 digest of the file contents, then truncate or otherwise represent it consistently as a 32-hex-character string. Make sure to do it the same way each time so Cloudflare can reliably match files across uploads.
103+
104+
#### API Response
105+
106+
If all the files are already stored on Cloudflare, the response will only return the JWT token. If new or updated files are needed, the response will return:
107+
108+
* `jwt`: An upload token (valid for 1 hour) which will be used in the API request to upload the file contents (Step 2).
109+
* `buckets`: An array of file-hash groups indicating which files to upload together. Files that have been recently uploaded won't appear in buckets, since Cloudflare already has them.
110+
111+
:::note
112+
This step alone does not store files on Cloudflare. You must upload the actual file data in the next step.
113+
:::
114+
115+
### 2. Upload File Contents
116+
117+
If the response to the Upload Session API returns `buckets`, that means you have new or changed files that need to be uploaded to Cloudflare.
118+
119+
Use the [Workers Assets Upload API](https://developers.cloudflare.com/api/resources/workers/subresources/assets/subresources/upload/) to transmit the raw file bytes in base64-encoded format for any missing or changed files. Once uploaded, Cloudflare will store these files so they can then be attached to a User Worker.
120+
121+
#### API Request Authentication
122+
123+
Unlike most Cloudflare API calls that use an account-wide API token in the Authorization header, uploading file contents requires using the short-lived JWT token returned in the `jwt` field of the `assets-upload-session` response.
124+
125+
Include it as a Bearer token in the header:
126+
127+
```bash
128+
Authorization: Bearer <upload-session-token>
129+
```
130+
131+
This token is valid for one hour and must be supplied for each upload request to the Workers Assets Upload API.
132+
133+
#### File fields (multipart/form-data)
134+
You must send the files as multipart/form-data with base64-encoded content:
135+
136+
* Field name: The file hash (for example, `36b8be012ee77df5f269b11b975611d3`)
137+
* Field value: A Base64-encoded string of the file's raw bytes
138+
139+
#### Example: Uploading multiple files within a single bucket
140+
141+
If your Upload Session response listed a single "bucket" containing two file hashes:
142+
143+
```json
144+
"buckets": [
145+
[
146+
"08f1dfda4574284ab3c21666d1ee8c7d4",
147+
"36b8be012ee77df5f269b11b975611d3"
148+
]
149+
]
150+
```
151+
152+
You can upload both files in one request, each as a form-data field:
153+
154+
```bash
155+
curl -X POST \
156+
"https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/workers/assets/upload?base64=true" \
157+
-H "Authorization: Bearer <upload-session-token>" \
158+
-F "08f1dfda4574284ab3c21666d1ee8c7d4=<BASE64_OF_INDEX_HTML>" \
159+
-F "36b8be012ee77df5f269b11b975611d3=<BASE64_OF_STYLES_CSS>"
160+
```
161+
162+
* `<upload-session-token>` is the token from step 1's assets-upload-session response
163+
* `<BASE64_OF_INDEX_HTML>` is the Base64-encoded content of index.html
164+
* `<BASE64_OF_STYLES_CSS>` is the Base64-encoded content of styles.css
165+
166+
If you have multiple buckets (for example, `[["hashA"], ["hashB"], ["hashC"]]`), you might need to repeat this process for each bucket, making one request per bucket group.
167+
168+
Once every file in the manifest has been uploaded, a status code of `201` will be returned, with the `jwt` field present. This JWT is a final "completion" token which can be used to create a deployment of a Worker with this set of assets. This completion token is valid for 1 hour.
169+
170+
```json
171+
{
172+
"success": true,
173+
"errors": [],
174+
"messages": [],
175+
"result": {
176+
"jwt": "<completion-token>"
177+
}
178+
}
179+
```
180+
181+
`<completion-token>` indicates that Cloudflare has successfully received and stored the file contents specified by your manifest. You will use this `<completion-token>` in Step 3 to finalize the attachment of these files to the Worker.
182+
183+
### 3. Deploy the User Worker with static assets
184+
185+
Now that Cloudflare has all the files it needs (from the previous upload steps), you must attach them to the User Worker by making a PUT request to the [Upload User Worker API](https://developers.cloudflare.com/api/resources/workers_for_platforms/subresources/dispatch/subresources/namespaces/subresources/scripts/methods/update/). This final step links the static assets to the User Worker using the completion token you received after uploading file contents.
186+
187+
You can also specify any optional settings under the `assets.config` field to customize how your files are served (for example, to handle trailing slashes in HTML paths).
188+
189+
#### API request example
190+
191+
```bash
192+
curl -X PUT \
193+
"https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/workers/dispatch/namespaces/$NAMESPACE_NAME/scripts/$SCRIPT_NAME" \
194+
-H "Content-Type: multipart/form-data" \
195+
-H "Authorization: Bearer $API_TOKEN" \
196+
-F 'metadata={
197+
"main_module": "index.js",
198+
"assets": {
199+
"jwt": "<completion-token>",
200+
"config": {
201+
"html_handling": "auto-trailing-slash"
202+
}
203+
},
204+
"compatibility_date": "2025-01-24"
205+
};type=application/json' \
206+
-F 'index.js=@/path/to/index.js;type=application/javascript'
207+
```
208+
209+
* The `"jwt": "<completion-token>"` links the newly uploaded files to the Worker
210+
* Including "html_handling" (or other fields under "config") is optional and can customize how static files are served
211+
* If the user's Worker code has not changed, you can omit the code file or re-upload the same index.js
212+
213+
Once this PUT request succeeds, the files are served on the User Worker. Requests routed to that Worker will serve the new or updated static assets.
214+
215+
---
216+
217+
## Deploying static assets with Wrangler
218+
219+
If you prefer a CLI-based approach and your platform setup allows direct publishing, you can use Wrangler to deploy both your Worker code and static assets. Wrangler bundles and uploads static assets (from a specified directory) along with your Worker script, so you can manage everything in one place.
220+
221+
Create or update your `wrangler.toml` to specify where Wrangler should look for static files:
222+
223+
```toml
224+
name = "my-static-site"
225+
main = "./src/index.js"
226+
compatibility_date = "2025-01-29"
227+
228+
[assets]
229+
directory = "./public"
230+
binding = "ASSETS"
231+
```
232+
233+
* `directory`: The local folder containing your static files (for example, `./public`).
234+
* `binding`: The binding name used to reference these assets within your Worker code.
235+
236+
### 1. Organize your files
237+
238+
Place your static files (HTML, CSS, images, etc.) in the specified directory (in this example, `./public`). Wrangler will detect and bundle these files when you publish your Worker.
239+
240+
If you need to reference these files in your Worker script to serve them dynamically, you can use the `ASSETS` binding like this:
241+
242+
```js
243+
export default {
244+
async fetch(request, env, ctx) {
245+
return env.ASSETS.fetch(request);
246+
},
247+
};
248+
```
249+
### 2. Deploy the User Worker with the static assets
250+
251+
Run Wrangler to publish both your Worker code and the static assets:
252+
253+
```bash
254+
npx wrangler deploy --name <USER_WORKER_NAME> --dispatch-namespace <NAMESPACE_NAME>
255+
```
256+
257+
Wrangler will automatically detect your static files, bundle them, and upload them to Cloudflare along with your Worker code.

0 commit comments

Comments
 (0)