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/workers/README.md
1# Cloudflare Workers23Expert guidance for building, deploying, and optimizing Cloudflare Workers applications.45## Overview67Cloudflare Workers run on V8 isolates (NOT containers/VMs):8- Extremely fast cold starts (< 1ms)9- Global deployment across 300+ locations10- Web standards compliant (fetch, URL, Headers, Request, Response)11- Support JS/TS, Python, Rust, and WebAssembly1213**Key principle**: Workers use web platform APIs wherever possible for portability.1415## Module Worker Pattern (Recommended)1617```typescript18export default {19async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {20return new Response('Hello World!');21},22};23```2425**Handler parameters**:26- `request`: Incoming HTTP request (standard Request object)27- `env`: Environment bindings (KV, D1, R2, secrets, vars)28- `ctx`: Execution context (`waitUntil`, `passThroughOnException`)2930## Essential Commands3132```bash33npx wrangler dev # Local dev34npx wrangler dev --remote # Remote dev (actual resources)35npx wrangler deploy # Production36npx wrangler deploy --env staging # Specific environment37npx wrangler tail # Stream logs38npx wrangler secret put API_KEY # Set secret39```4041## When to Use Workers4243- API endpoints at the edge44- Request/response transformation45- Authentication/authorization layers46- Static asset optimization47- A/B testing and feature flags48- Rate limiting and security49- Proxy/routing logic50- WebSocket applications5152## Quick Start5354```bash55npm create cloudflare@latest my-worker -- --type hello-world56cd my-worker57npx wrangler dev58```5960## Handler Signatures6162```typescript63// HTTP requests64async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response>6566// Cron triggers67async scheduled(event: ScheduledEvent, env: Env, ctx: ExecutionContext): Promise<void>6869// Queue consumer70async queue(batch: MessageBatch, env: Env, ctx: ExecutionContext): Promise<void>7172// Tail consumer73async tail(events: TraceItem[], env: Env, ctx: ExecutionContext): Promise<void>74```7576## Resources7778**Docs**: https://developers.cloudflare.com/workers/79**Examples**: https://developers.cloudflare.com/workers/examples/80**Runtime APIs**: https://developers.cloudflare.com/workers/runtime-apis/8182## In This Reference8384- [Configuration](./configuration.md) - wrangler.jsonc setup, bindings, environments85- [API](./api.md) - Runtime APIs, bindings, execution context86- [Patterns](./patterns.md) - Common workflows, testing, optimization87- [Frameworks](./frameworks.md) - Hono, routing, validation88- [Gotchas](./gotchas.md) - Common issues, limits, troubleshooting8990## Reading Order9192| Task | Start With | Then Read |93|------|------------|-----------|94| First Worker | README → Configuration → API | Patterns |95| Add framework | Frameworks | Configuration (bindings) |96| Add storage/bindings | Configuration → API (binding usage) | See Also links |97| Debug issues | Gotchas | API (specific binding docs) |98| Production optimization | Patterns | API (caching, streaming) |99| Type safety | Configuration (TypeScript) | Frameworks (Hono typing) |100101## See Also102103- [KV](../kv/README.md) - Key-value storage104- [D1](../d1/README.md) - SQL database105- [R2](../r2/README.md) - Object storage106- [Durable Objects](../durable-objects/README.md) - Stateful coordination107- [Queues](../queues/README.md) - Message queues108- [Wrangler](../wrangler/README.md) - CLI tool reference109