Skip to main content

Overview

Cloudflare Workers Assets allows you to deploy static files alongside your Worker code. The asset management system handles uploading, caching, and serving static content with automatic optimization and CDN distribution.
Assets are uploaded separately from your Worker bundle and can include HTML, CSS, JavaScript, images, and other static files up to 25 MiB per file.

Configuration

Basic Setup

Configure assets in your wrangler.json file:
wrangler.json

Configuration Options

string
required
Path to the directory containing static assets. Can be relative to the config file or absolute.
string
Variable name to access assets from your Worker code. If omitted, assets are served directly without Worker involvement.
string
Controls how HTML files are served:
  • auto-trailing-slash - Automatically add trailing slashes to directory URLs
  • force-trailing-slash - Redirect all requests to include trailing slashes
  • drop-trailing-slash - Remove trailing slashes from URLs
  • none - Serve HTML files as-is
string
Behavior when a requested asset is not found:
  • single-page-application - Serve index.html for all 404s (SPA mode)
  • 404-page - Serve a custom 404.html page
  • none - Return standard 404 response
boolean | string[]
Controls routing between Worker and assets:
  • true - All requests go to Worker first; use binding to fetch assets
  • false - Assets are served first; Worker handles non-asset routes
  • Array of patterns - Specific routes that should invoke Worker before assets

Asset Upload Process

The asset upload system uses an intelligent incremental upload mechanism:

1. Manifest Generation

2. Incremental Upload

Only new or modified files are uploaded. The system:
  • Compares hashes with previously deployed assets
  • Uploads only changed files
  • Organizes uploads into buckets (max 50 MiB each)
  • Uses concurrent uploads (3 buckets at a time)
  • Implements exponential backoff on failures

3. Size Limits

Individual asset files must not exceed 25 MiB. Files larger than this limit will cause deployment to fail.

Ignoring Files

Create a .assetsignore file in your asset directory to exclude files from upload:
.assetsignore
The ignore syntax follows .gitignore patterns. Files matching these patterns won’t be uploaded to Cloudflare.

Worker Integration

Assets Binding

Access assets from your Worker using the configured binding:
src/index.ts

Custom Routing

Use run_worker_first with patterns for fine-grained control:
wrangler.json
With this configuration:
  • /api/*, /admin/*, /auth/* routes invoke your Worker first
  • All other routes serve assets directly
  • Worker can fall back to assets using the binding

Headers and Redirects

Add custom headers or redirects using special files: _headers
public/_headers
_redirects
public/_redirects

Command Line Usage

Deploy with Assets

Asset Information

During deployment, Wrangler displays upload progress:

Best Practices

Optimize Assets

Compress images, minify CSS/JS, and use modern formats (WebP, Brotli) before uploading.

Cache Strategy

Use _headers to set appropriate cache headers for different file types.

Security

Never commit sensitive files. Use .assetsignore to exclude .env and credential files.

Smart Placement

Avoid using Smart Placement with run_worker_first: true as it may increase latency for asset serving.

Validation Rules

Incompatible FeaturesAssets cannot be used with:
  • Workers Sites (legacy feature)
  • Tail consumers
  • Assets-only Workers with bindings (must have a Worker script to use bindings)

Error Handling

Common Errors

Asset directory does not exist
Solution: Verify the path is correct and the directory exists. Asset too large
Solution: Reduce file size or host large files elsewhere (e.g., R2). Legacy _worker.js detected
Solution: Add _worker.js to .assetsignore or remove the file.

Advanced Examples

SPA with API Routes

wrangler.json
src/index.ts

Static Site with Custom 404

wrangler.json
Create public/404.html for custom error pages.