Write and optimize Cloudflare Workers — implement fetch handlers, Durable Objects for stateful logic, service bindings, KV/R2 data access, scheduled/cron triggers, and middleware patterns with wrangler dev/deploy. Use when writing Worker code, debugging Worker runtime behavior, or designing Durable Object state machines. Do not use for Cloudflare DNS/WAF/Pages configuration (prefer cloudflare skill) or non-Worker serverless platforms.
Write, structure, and optimize Cloudflare Worker application code — fetch handlers, Durable Objects for stateful coordination, service bindings for Worker-to-Worker calls, KV/R2/D1 data access patterns, scheduled/cron handlers, and middleware/router patterns using the Workers runtime.
fetch handler that routes requests, transforms responses, or proxies origins.scheduled event handlers for cron-triggered background tasks.wrangler dev and wrangler tail.wrangler test (Vitest integration).cloudflare.serverless-patterns.wrangler.toml bindings without any Worker code changes — prefer cloudflare.src/index.ts (or .js) file exported as the Worker module. Confirm it exports a fetch handler and optionally scheduled, queue, or email handlers.wrangler dev to start the local development server. Confirm bindings (KV, R2, D1, Durable Objects) are available via --local or --remote flags.Request URL and method. Route to handler functions using a router (Hono: app.get('/path', handler), itty-router: router.get('/path', handler)). Return a new Response() with appropriate status, headers, and body.Access-Control-Allow-Origin), authentication (verify JWT or API key from Authorization header), request logging (timestamp, method, path, status), and error wrapping (try/catch returning 500 with error ID).DurableObject. Implement fetch() for HTTP-based state access. Use this.ctx.storage.get/put/delete for persistent state. Use this.ctx.storage.transaction() for atomic multi-key operations. Bind the DO in wrangler.toml under [durable_objects].env.MY_KV.get(key) / .put(key, value, {expirationTtl}). For R2: use env.MY_BUCKET.get(key) / .put(key, body). For D1: use env.MY_DB.prepare('SELECT ...').bind(params).all(). Handle null returns (key not found) explicitly.scheduled(event, env, ctx) function. Use event.cron to distinguish between multiple cron triggers. Use ctx.waitUntil() for async work that must complete after the handler returns.wrangler.toml, add [[services]] with binding, service, and environment. Call the bound service from Worker code via env.MY_SERVICE.fetch(request).ctx.waitUntil() for fire-and-forget work (analytics, logging) — do not await it in the response path.fetch('https://other-worker.example.com') to avoid network overhead and enforce internal-only access.fetch/scheduled handler implementation with proper typing.references/preflight-checklist.mdcloudflare — DNS, WAF, Pages, R2/KV/D1 provisioning and wrangler.toml configuration.serverless-patterns — generic serverless architecture patterns.vercel — alternative edge runtime platform.ctx.waitUntil() for background tasks.fetch() to call another Worker in the same account instead of service bindings.null return from KV.get() or R2.get() — always check for missing keys.wrangler dev fails to start, check that wrangler.toml bindings have valid IDs and that node_modules is installed.wrangler deploy succeeds but the Worker returns errors, check wrangler tail for unhandled exceptions and verify all environment bindings are provisioned in the target environment.cloudflare skill.wrangler testfetchMockenv.MY_DO.get(id)wrangler tail to stream live logs. Check for unhandled promise rejections that silently fail. Verify ctx.waitUntil() is used for background work.wrangler deploy. Hit the production URL and verify responses. Check wrangler tail --format=json for errors in production traffic.