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/configuration.md
1## Configuration23### Basic Setup45Minimal configuration requires only `assets.directory`:67```jsonc8{9"name": "my-worker",10"compatibility_date": "2025-01-01", // Use current date for new projects11"assets": {12"directory": "./dist"13}14}15```1617### Full Configuration Options1819```jsonc20{21"name": "my-worker",22"main": "src/index.ts",23"compatibility_date": "2025-01-01",24"assets": {25"directory": "./dist",26"binding": "ASSETS",27"not_found_handling": "single-page-application",28"html_handling": "auto-trailing-slash",29"run_worker_first": ["/api/*", "!/api/docs/*"]30}31}32```3334**Configuration keys:**3536- `directory` (string, required): Path to assets folder (e.g. `./dist`, `./public`, `./build`)37- `binding` (string, optional): Name to access assets in Worker code (e.g. `env.ASSETS`). Default: `"ASSETS"`38- `not_found_handling` (string, optional): Behavior when asset not found39- `"single-page-application"`: Serve `/index.html` for non-asset paths (default for SPAs)40- `"404-page"`: Serve `/404.html` if present, otherwise 40441- `"none"`: Return 404 for missing assets42- `html_handling` (string, optional): URL trailing slash behavior43- `run_worker_first` (boolean | string[], optional): Routes that invoke Worker before checking assets4445### not_found_handling Modes4647| Mode | Behavior | Use Case |48|------|----------|----------|49| `"single-page-application"` | Serve `/index.html` for non-asset requests | React, Vue, Angular SPAs |50| `"404-page"` | Serve `/404.html` if exists, else 404 | Static sites with custom error page |51| `"none"` | Return 404 for missing assets | API-first or custom routing |5253### html_handling Modes5455Controls trailing slash behavior for HTML files:5657| Mode | `/page` | `/page/` | Use Case |58|------|---------|----------|----------|59| `"auto-trailing-slash"` | Redirect to `/page/` if `/page/index.html` exists | Serve `/page/index.html` | Default, SEO-friendly |60| `"force-trailing-slash"` | Always redirect to `/page/` | Serve if exists | Consistent trailing slashes |61| `"drop-trailing-slash"` | Serve if exists | Redirect to `/page` | Cleaner URLs |62| `"none"` | No modification | No modification | Custom routing logic |6364**Default:** `"auto-trailing-slash"`6566### run_worker_first Configuration6768Controls which requests invoke Worker before checking assets.6970**Boolean syntax:**7172```jsonc73{74"assets": {75"run_worker_first": true // ALL requests invoke Worker76}77}78```7980**Array syntax (recommended):**8182```jsonc83{84"assets": {85"run_worker_first": [86"/api/*", // Positive pattern: match API routes87"/admin/*", // Match admin routes88"!/admin/assets/*" // Negative pattern: exclude admin assets89]90}91}92```9394**Pattern rules:**9596- Glob patterns: `*` (any chars), `**` (any path segments)97- Negative patterns: Prefix with `!` to exclude98- Precedence: Negative patterns override positive patterns99- Default: `false` (assets served directly)100101**Decision guidance:**102103- Use `true` for API-first apps (few static assets)104- Use array patterns for hybrid apps (APIs + static assets)105- Use `false` for static-first sites (minimal dynamic routes)106107### .assetsignore File108109Exclude files from upload using `.assetsignore` (same syntax as `.gitignore`):110111```112# .assetsignore113_worker.js114*.map115*.md116node_modules/117.git/118```119120**Common patterns:**121122- `_worker.js` - Exclude Worker code from assets123- `*.map` - Exclude source maps124- `*.md` - Exclude markdown files125- Development artifacts126127### Vite Plugin Integration128129For Vite-based projects, use `@cloudflare/vite-plugin`:130131```typescript132// vite.config.ts133import { defineConfig } from 'vite';134import { cloudflare } from '@cloudflare/vite-plugin';135136export default defineConfig({137plugins: [138cloudflare({139assets: {140directory: './dist',141binding: 'ASSETS'142}143})144]145});146```147148**Features:**149150- Automatic asset detection during dev151- Hot module replacement for assets152- Production build integration153- Requires: Wrangler 4.0.0+, `@cloudflare/vite-plugin` 1.0.0+154155### Key Compatibility Dates156157| Date | Feature | Impact |158|------|---------|--------|159| `2025-04-01` | Navigation request optimization | SPAs skip Worker for navigation, reducing costs |160161Use current date for new projects. See [Compatibility Dates](https://developers.cloudflare.com/workers/configuration/compatibility-dates/) for full list.162163### Environment-Specific Configuration164165Use `wrangler.jsonc` environments for different configs:166167```jsonc168{169"name": "my-worker",170"assets": { "directory": "./dist" },171"env": {172"staging": {173"assets": {174"not_found_handling": "404-page"175}176},177"production": {178"assets": {179"not_found_handling": "single-page-application"180}181}182}183}184```185186Deploy with: `wrangler deploy --env staging`187