Skip to content

Commit 127ac63

Browse files
korinnehyperlint-ai[bot]GregBrimble
authored
Adds tip and warning to Workers for Platforms docs, regarding how to isolate static assets on upload (#20601)
* Adds warning under Create an Upload Session * Moves previous warning to Upload File Contents section, puts tip and recs under Create an Upload Session section. * 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: Greg Brimble <[email protected]> * Update src/content/docs/cloudflare-for-platforms/workers-for-platforms/configuration/static-assets.mdx Co-authored-by: Greg Brimble <[email protected]> * Update src/content/docs/cloudflare-for-platforms/workers-for-platforms/configuration/static-assets.mdx Co-authored-by: Greg Brimble <[email protected]> --------- Co-authored-by: hyperlint-ai[bot] <154288675+hyperlint-ai[bot]@users.noreply.github.com> Co-authored-by: Greg Brimble <[email protected]>
1 parent 73be140 commit 127ac63

File tree

1 file changed

+57
-35
lines changed
  • src/content/docs/cloudflare-for-platforms/workers-for-platforms/configuration

1 file changed

+57
-35
lines changed

src/content/docs/cloudflare-for-platforms/workers-for-platforms/configuration/static-assets.mdx

Lines changed: 57 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,33 @@ pcx_content_type: concept
33
title: Static assets
44
description: Host static assets on Cloudflare's global network and deliver faster load times worldwide with Workers for Platforms.
55
---
6+
7+
import { Aside } from "@astrojs/starlight/components";
8+
69
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.
710

811
### What you can build
912

1013
#### Static sites
14+
1115
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.
1216

1317
#### Full-stack applications
18+
1419
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.
1520

1621
### Benefits
1722

1823
#### Global caching for faster performance
24+
1925
Cloudflare automatically caches static assets at data centers worldwide, reducing latency and improving load times by up to 2x for users everywhere.
2026

2127
#### Scalability without infrastructure management
28+
2229
Your applications scale automatically to handle high traffic without requiring you to provision or manage infrastructure. Cloudflare dynamically adjusts to demand in real time.
2330

2431
#### Unified deployment for static and dynamic content
32+
2533
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.
2634

2735
---
@@ -45,22 +53,29 @@ After these steps are completed, the User Worker's static assets will be live on
4553

4654
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:
4755

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
56+
- A file path (for example, `"/index.html"` or `"/assets/logo.png"`)
57+
- A hash (32-hex characters) representing the file contents
58+
- The file size in bytes
59+
60+
<Aside type="note" title="Asset Isolation Considerations">
61+
Static assets uploaded to Workers for Platforms are associated with the namespace rather than with individual User Worker. If multiple User Workers exist under the same namespace, assets with identical hashes may be shared across them. **JWTs should therefore only be shared with trusted platform services and should never be distributed to end-users.**
62+
63+
If strict isolation of assets is required, we recommend either salting with a random value each time, or incorporating an end-user identifier (e.g. account ID or Worker script ID) within the hashing process, to ensure uniqueness. For example, `hash = slice(sha256(accountID + fileContents), 32)`.
64+
65+
</Aside>
5166

5267
#### Example manifest (JSON)
5368

5469
```json
5570
{
56-
"/index.html": {
57-
"hash": "08f1dfda4574284ab3c21666d1ee8c7d4",
58-
"size": 1234
59-
},
60-
"/styles.css": {
61-
"hash": "36b8be012ee77df5f269b11b975611d3",
62-
"size": 5678
63-
}
71+
"/index.html": {
72+
"hash": "08f1dfda4574284ab3c21666d1ee8c7d4",
73+
"size": 1234
74+
},
75+
"/styles.css": {
76+
"hash": "36b8be012ee77df5f269b11b975611d3",
77+
"size": 5678
78+
}
6479
}
6580
```
6681

@@ -71,8 +86,9 @@ POST /accounts/{account_id}/workers/dispatch/namespaces/{namespace}/scripts/{scr
7186
```
7287

7388
Path Parameters:
74-
* `namespace`: Name of the Workers for Platforms dispatch namespace
75-
* `script_name`: Name of the User Worker
89+
90+
- `namespace`: Name of the Workers for Platforms dispatch namespace
91+
- `script_name`: Name of the User Worker
7692

7793
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.
7894

@@ -105,8 +121,8 @@ You can compute a SHA-256 digest of the file contents, then truncate or otherwis
105121

106122
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:
107123

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.
124+
- `jwt`: An upload token (valid for 1 hour) which will be used in the API request to upload the file contents (Step 2).
125+
- `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.
110126

111127
:::note
112128
This step alone does not store files on Cloudflare. You must upload the actual file data in the next step.
@@ -118,6 +134,10 @@ If the response to the Upload Session API returns `buckets`, that means you have
118134

119135
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.
120136

137+
<Aside type="caution">
138+
Asset uniqueness is determined by the provided hash and are associated globally to their namespace rather than with each specific User Worker. If an asset has already been uploaded for that namespace earlier, Cloudflare will automatically omit sending this asset hash back in the `buckets` response to save you from re-uploading the same thing twice. This means that an asset can be shared between multiple User Workers if it shares the same hash unless you **explicitly make the hash unique**. If you require full isolation between assets across User Workers, incorporate a unique identifier within your asset hashing process (either salting it with something entirely random each time, or by including the end-user account ID or their Worker name to retain per-customer re-use).
139+
</Aside>
140+
121141
#### API Request Authentication
122142

123143
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.
@@ -131,10 +151,11 @@ Authorization: Bearer <upload-session-token>
131151
This token is valid for one hour and must be supplied for each upload request to the Workers Assets Upload API.
132152

133153
#### File fields (multipart/form-data)
154+
134155
You must send the files as multipart/form-data with base64-encoded content:
135156

136-
* Field name: The file hash (for example, `36b8be012ee77df5f269b11b975611d3`)
137-
* Field value: A Base64-encoded string of the file's raw bytes
157+
- Field name: The file hash (for example, `36b8be012ee77df5f269b11b975611d3`)
158+
- Field value: A Base64-encoded string of the file's raw bytes
138159

139160
#### Example: Uploading multiple files within a single bucket
140161

@@ -159,22 +180,22 @@ curl -X POST \
159180
-F "36b8be012ee77df5f269b11b975611d3=<BASE64_OF_STYLES_CSS>"
160181
```
161182

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
183+
- `<upload-session-token>` is the token from step 1's assets-upload-session response
184+
- `<BASE64_OF_INDEX_HTML>` is the Base64-encoded content of index.html
185+
- `<BASE64_OF_STYLES_CSS>` is the Base64-encoded content of styles.css
165186

166187
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.
167188

168189
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.
169190

170191
```json
171192
{
172-
"success": true,
173-
"errors": [],
174-
"messages": [],
175-
"result": {
176-
"jwt": "<completion-token>"
177-
}
193+
"success": true,
194+
"errors": [],
195+
"messages": [],
196+
"result": {
197+
"jwt": "<completion-token>"
198+
}
178199
}
179200
```
180201

@@ -206,9 +227,9 @@ curl -X PUT \
206227
-F 'index.js=@/path/to/index.js;type=application/javascript'
207228
```
208229

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
230+
- The `"jwt": "<completion-token>"` links the newly uploaded files to the Worker
231+
- Including "html_handling" (or other fields under "config") is optional and can customize how static files are served
232+
- If the user's Worker code has not changed, you can omit the code file or re-upload the same index.js
212233

213234
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.
214235

@@ -236,8 +257,8 @@ binding = "ASSETS"
236257

237258
</WranglerConfig>
238259

239-
* `directory`: The local folder containing your static files (for example, `./public`).
240-
* `binding`: The binding name used to reference these assets within your Worker code.
260+
- `directory`: The local folder containing your static files (for example, `./public`).
261+
- `binding`: The binding name used to reference these assets within your Worker code.
241262

242263
### 1. Organize your files
243264

@@ -247,11 +268,12 @@ If you need to reference these files in your Worker script to serve them dynamic
247268

248269
```js
249270
export default {
250-
async fetch(request, env, ctx) {
251-
return env.ASSETS.fetch(request);
252-
},
271+
async fetch(request, env, ctx) {
272+
return env.ASSETS.fetch(request);
273+
},
253274
};
254275
```
276+
255277
### 2. Deploy the User Worker with the static assets
256278

257279
Run Wrangler to publish both your Worker code and the static assets:
@@ -260,4 +282,4 @@ Run Wrangler to publish both your Worker code and the static assets:
260282
npx wrangler deploy --name <USER_WORKER_NAME> --dispatch-namespace <NAMESPACE_NAME>
261283
```
262284

263-
Wrangler will automatically detect your static files, bundle them, and upload them to Cloudflare along with your Worker code.
285+
Wrangler will automatically detect your static files, bundle them, and upload them to Cloudflare along with your Worker code.

0 commit comments

Comments
 (0)