Static Assets SPA
Workers Static Assets with ASSETS binding and run_worker_first
Serve a static SPA from Workers Static Assets while /api/* routes run on the Worker first via run_worker_first. Uses the ASSETS binding for programmatic asset fetches.
Features
- Static files from
./publicwith SPAnot_found_handling run_worker_first: ["/api/*"]so API hits the Worker before assetsGET /api/helloandGET /api/infoJSON endpoints- Catch-all can proxy to
env.ASSETS.fetchwhen the Worker handles non-API paths
API Reference
GET /api/hello
Returns a hello payload from Worker code.
Example Request
curl "https://your-worker.workers.dev/api/hello"Success Response
{
"message": "Hello from the Worker",
"servedBy": "worker"
}GET /api/info
App metadata including whether the ASSETS binding is present.
{
"name": "static-assets-spa",
"description": "Workers Static Assets SPA with ASSETS binding and run_worker_first for API routes",
"assetsBinding": true
}GET /
In unit tests (no assets runtime), returns JSON app info. In production, / is typically served as public/index.html because only /api/* uses run_worker_first.
Use Cases
- Ship a small SPA + JSON API as one Worker
- Authenticate or rewrite API routes while keeping static files edge-cached
- Learn
ASSETSbinding andrun_worker_firstrouting
Limitations
- Only one assets directory per Worker
- Smoke tests call the Worker fetch handler directly (JSON
/), which differs from production asset-first/ - SPA client must call same-origin
/api/*routes
Deployment
Confirm assets config
wrangler.json includes assets.directory, binding: "ASSETS", not_found_handling: "single-page-application", and run_worker_first: ["/api/*"].
Test your deployment
Open the Worker URL for the SPA, or:
curl "https://your-worker.workers.dev/api/hello"Local Development
cd apps/experiments/static-assets-spa
npm install
npm run devOpen the local URL to load the SPA; it fetches /api/hello.
Configuration
"assets": {
"directory": "./public",
"binding": "ASSETS",
"not_found_handling": "single-page-application",
"run_worker_first": ["/api/*"]
}Cloudflare Features Used
- Workers - Edge compute runtime
- Static Assets - Globally cached frontend files
- Assets binding -
env.ASSETS.fetchandrun_worker_first