Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Comprehensive Cloudflare platform skill covering Workers, D1, R2, KV, AI, Durable Objects, and security.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/static-assets/api.md
1# API Reference23## ASSETS Binding45The `ASSETS` binding provides access to static assets via the `Fetcher` interface.67### Type Definition89```typescript10interface Env {11ASSETS: Fetcher;12}1314interface Fetcher {15fetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response>;16}17```1819### Method Signatures2021```typescript22// 1. Forward entire request23await env.ASSETS.fetch(request);2425// 2. String path (hostname ignored, only path matters)26await env.ASSETS.fetch("https://any-host/path/to/asset.png");2728// 3. URL object29await env.ASSETS.fetch(new URL("/index.html", request.url));3031// 4. Constructed Request object32await env.ASSETS.fetch(new Request(new URL("/logo.png", request.url), {33method: "GET",34headers: request.headers35}));36```3738**Key behaviors:**3940- Host/origin is ignored for string/URL inputs (only path is used)41- Method must be GET (others return 405)42- Request headers pass through (affects response)43- Returns standard `Response` object4445## Request Handling4647### Path Resolution4849```typescript50// All resolve to same asset:51env.ASSETS.fetch("https://example.com/logo.png")52env.ASSETS.fetch("https://ignored.host/logo.png")53env.ASSETS.fetch("/logo.png")54```5556Assets are resolved relative to configured `assets.directory`.5758### Headers5960Request headers that affect response:6162| Header | Effect |63|--------|--------|64| `Accept-Encoding` | Controls compression (gzip, brotli) |65| `Range` | Enables partial content (206 responses) |66| `If-None-Match` | Conditional request via ETag |67| `If-Modified-Since` | Conditional request via modification date |6869Custom headers pass through but don't affect asset serving.7071### Method Support7273| Method | Supported | Response |74|--------|-----------|----------|75| `GET` | ✅ Yes | Asset content |76| `HEAD` | ✅ Yes | Headers only, no body |77| `POST`, `PUT`, etc. | ❌ No | 405 Method Not Allowed |7879## Response Behavior8081### Content-Type Inference8283Automatically set based on file extension:8485| Extension | Content-Type |86|-----------|--------------|87| `.html` | `text/html; charset=utf-8` |88| `.css` | `text/css` |89| `.js` | `application/javascript` |90| `.json` | `application/json` |91| `.png` | `image/png` |92| `.jpg`, `.jpeg` | `image/jpeg` |93| `.svg` | `image/svg+xml` |94| `.woff2` | `font/woff2` |9596### Default Headers9798Responses include:99100```101Content-Type: <inferred>102ETag: "<hash>"103Cache-Control: public, max-age=3600104Content-Encoding: br (if supported and beneficial)105```106107**Cache-Control defaults:**108109- 1 hour (`max-age=3600`) for most assets110- Override via Worker response transformation (see patterns.md:27-35)111112### Compression113114Automatic compression based on `Accept-Encoding`:115116- **Brotli** (`br`): Preferred, best compression117- **Gzip** (`gzip`): Fallback118- **None**: If client doesn't support or asset too small119120### ETag Generation121122ETags are content-based hashes:123124```125ETag: "a3b2c1d4e5f6..."126```127128Used for conditional requests (`If-None-Match`). Returns `304 Not Modified` if match.129130## Error Responses131132| Status | Condition | Behavior |133|--------|-----------|----------|134| `404` | Asset not found | Body depends on `not_found_handling` config |135| `405` | Non-GET/HEAD method | `{ "error": "Method not allowed" }` |136| `416` | Invalid Range header | Range not satisfiable |137138### 404 Handling139140Depends on configuration (see configuration.md:45-52):141142```typescript143// not_found_handling: "single-page-application"144// Returns /index.html with 200 status145146// not_found_handling: "404-page"147// Returns /404.html if exists, else 404 response148149// not_found_handling: "none"150// Returns 404 response151```152153## Advanced Usage154155### Modifying Responses156157```typescript158const response = await env.ASSETS.fetch(request);159160// Clone and modify161return new Response(response.body, {162status: response.status,163headers: {164...Object.fromEntries(response.headers),165'Cache-Control': 'public, max-age=31536000',166'X-Custom': 'value'167}168});169```170171See patterns.md:27-35 for full example.172173### Error Handling174175```typescript176const response = await env.ASSETS.fetch(request);177178if (!response.ok) {179// Asset not found or error180return new Response('Custom error page', { status: 404 });181}182183return response;184```185186### Conditional Serving187188```typescript189const url = new URL(request.url);190191// Serve different assets based on conditions192if (url.pathname === '/') {193return env.ASSETS.fetch('/index.html');194}195196return env.ASSETS.fetch(request);197```198199See patterns.md for complete patterns.200