workers-best-practices

โดย Cloudflare

ตรวจสอบและเขียนโค้ด Cloudflare Workers ตามแนวทางปฏิบัติที่ดีที่สุดสำหรับโปรดักชัน ใช้เมื่อเขียน Workers ใหม่ ตรวจสอบโค้ด Worker กำหนดค่า wrangler.jsonc หรือตรวจสอบรูปแบบที่ไม่เหมาะสมของ Workers ทั่วไป (การสตรีม, floating promises, global state, secrets, bindings, observability) เน้นการดึงข้อมูลจากเอกสารของ Cloudflare มากกว่าความรู้ที่ฝึกไว้ล่วงหน้า

npx skills add https://github.com/cloudflare/skills --skill workers-best-practices

Your knowledge of Cloudflare Workers APIs, types, and configuration may be outdated. Prefer retrieval over pre-training when writing or reviewing Workers code.

Use the project's installed versions, generated types, and Wrangler compatibility settings as the baseline for existing code. Retrieve relevant Cloudflare documentation to verify API, configuration, runtime behavior, and limit claims.

References

Read the sections relevant to the task:

ReferenceWhen to use it
Configuration and observabilityCompatibility dates, bindings, generated types, secrets, logs, and traces
Runtime patternsStreaming, promise lifetime, request state, service calls, security, and runtime tests
Platform API checksHandler signatures, platform classes, binding access, and serialization

For missing evidence, consult Workers best practices or find the affected product in the Cloudflare docs directory. Use the installed Wrangler schema for config fields. A newer type package does not supersede the project's configured target.

Keep Compatibility Dates Current

Use today's date for new Workers. Encourage periodic updates for existing Workers, reviewing compatibility changes and running relevant tests. Assess existing behavior against its configured date and flags; see compatibility guidance.

Enable Observability

Enable Workers Logs and Traces when creating or preparing a Worker for production. Set observability.enabled and observability.traces.enabled to true; the top-level setting alone does not enable traces. Use structured JSON logging and configure sampling for the workload. During reviews, flag missing logs or traces. See the configuration example.

Anti-Patterns to Flag

Anti-patternConsequence and preferred pattern
await response.text() or similar buffering on unbounded dataCan exhaust Worker memory; stream large or unbounded bodies.
Hardcoded secrets in source or configLeaks credentials through version control; use Wrangler secrets.
Math.random() for security-sensitive tokens or IDsPredictable values; use crypto.randomUUID() or crypto.getRandomValues().
Async work started without awaiting, returning, or attaching it to ctx.waitUntil()Work can be dropped and errors missed; tie it to the request or background-work lifetime.
Module-level mutable request stateLeaks data across requests and can cause I/O ownership errors; pass request state explicitly.
Cloudflare REST API calls for operations available through Worker bindingsAdds network and authentication overhead; use the available binding.
ctx.passThroughOnException() used as general error handlingCan conceal Worker failures by forwarding to the origin; use explicit error handling and structured error responses.
Hand-written Env that duplicates Wrangler bindingsCan drift from configuration; generate binding types with wrangler types.
Direct string comparison of secret valuesCan expose timing differences; use the Web Crypto comparison pattern.
Destructuring ctx methods, such as const { waitUntil } = ctxLoses the receiver; call ctx.waitUntil(...).
any on Env or handler parametersHides binding and handler contract errors; use the project's generated and platform types.
as unknown as T to force a platform type matchHides incompatibilities; fix the underlying contract.
implements used in place of extending a platform base classDoes not inherit runtime behavior, this.ctx, or this.env; use the appropriate base class.
Unbound env.X in a platform class methodBindings are available through this.env.X; see binding access patterns.
Applying one serialization rule across Queues, Workflow steps, storage, and WebSocketsCan reject valid payloads or accept unsupported ones; check the specific API and encoding.

Validation

Use the project's existing checks for affected Workers behavior: type-check binding or handler contract changes, and run relevant runtime tests for behavior changes. Preserve required repository checks; a narrow edit does not require a full Workers audit.

Scope

This skill covers Workers-specific best practices and code review. For related topics:

  • Durable Objects: load the durable-objects skill
  • Workflows: see Rules of Workflows
  • Wrangler CLI commands: load the wrangler skill

Skills เพิ่มเติมจาก Cloudflare

agents-sdk
Cloudflare
สร้าง AI agents บน Cloudflare Workers โดยใช้ Agents SDK โหลดเมื่อสร้าง stateful agents, durable workflows, แอป WebSocket แบบเรียลไทม์, งานตามกำหนดเวลา, MCP servers หรือแอปแชท ครอบคลุม Agent class, การจัดการสถานะ, RPC ที่เรียกได้, การรวม Workflows และ React hooks
building-ai-agent-on-cloudflare
Cloudflare
| สร้างเอเจนต์ AI บน Cloudflare โดยใช้ Agents SDK พร้อมการจัดการสถานะ WebSocket แบบเรียลไทม์ งานตามกำหนดเวลา การรวมเครื่องมือ และความสามารถในการแชท สร้างโค้ดเอเจนต์ที่พร้อมใช้งานจริงซึ่งปรับใช้กับ Workers ใช้เมื่อ: ผู้ใช้ต้องการ "สร้างเอเจนต์", "เอเจนต์ AI", "เอเจนต์แชท", "เอเจนต์ ที่มีสถานะ", พูดถึง "Agents SDK", ต้องการ "AI แบบเรียลไทม์", "WebSocket AI", หรือถามเกี่ยวกับ "การจัดการสถานะ" ของเอเจนต์, "งานตามกำหนดเวลา" หรือ "การเรียกใช้เครื่องมือ
development
building-mcp-server-on-cloudflare
Cloudflare
| สร้างเซิร์ฟเวอร์ MCP (Model Context Protocol) ระยะไกลบน Cloudflare Workers พร้อมเครื่องมือ การตรวจสอบสิทธิ์ OAuth และการปรับใช้ในระบบผลิต สร้างโค้ดเซิร์ฟเวอร์ กำหนดค่าผู้ให้บริการตรวจสอบสิทธิ์ และปรับใช้กับ Workers ใช้เมื่อ: ผู้ใช้ต้องการ "สร้างเซิร์ฟเวอร์ MCP", "สร้างเครื่องมือ MCP", "MCP ระยะไกล", "ปรับใช้ MCP", เพิ่ม "OAuth ให้กับ MCP" หรือกล่าวถึง Model Context Protocol บน Cloudflare นอกจากนี้ยังทำงานเมื่อมี "การตรวจสอบสิทธิ์ MCP" หรือ "การปรับใช้ MCP
development
cloudflare
Cloudflare
ทักษะที่ครอบคลุมแพลตฟอร์ม Cloudflare ครอบคลุม Workers, Pages, พื้นที่จัดเก็บ (KV, D1, R2), AI (Workers AI, Vectorize, Agents SDK), เครือข่าย (Tunnel, Spectrum), ความปลอดภัย (WAF, DDoS) และโครงสร้างพื้นฐานเป็นโค้ด (Terraform, Pulumi) ใช้สำหรับงานพัฒนา Cloudflare ใดๆ
durable-objects
Cloudflare
สร้างและตรวจสอบ Cloudflare Durable Objects ใช้เมื่อสร้างการประสานงานที่มีสถานะ (ห้องแชท เกมผู้เล่นหลายคน ระบบจอง) การใช้งานเมธอด RPC พื้นที่จัดเก็บ SQLite สัญญาณเตือน WebSockets หรือตรวจสอบโค้ด DO เพื่อแนวทางปฏิบัติที่ดีที่สุด ครอบคลุมการรวม Workers การกำหนดค่า wrangler และการทดสอบด้วย Vitest
sandbox-sdk
Cloudflare
สร้างแอปพลิเคชันในสภาพแวดล้อมแซนด์บ็อกซ์สำหรับการรันโค้ดอย่างปลอดภัย โหลดเมื่อสร้างการรันโค้ด AI, ตัวแปลโค้ด, ระบบ CI/CD, สภาพแวดล้อมการพัฒนาที่โต้ตอบได้ หรือการรันโค้ดที่ไม่น่าเชื่อถือ ครอบคลุมวงจรชีวิตของ Sandbox SDK, คำสั่ง, ไฟล์, ตัวแปลโค้ด และ URL แสดงตัวอย่าง
web-perf
Cloudflare
วิเคราะห์ประสิทธิภาพเว็บด้วย Chrome DevTools MCP วัด Core Web Vitals (FCP, LCP, TBT, CLS, Speed Index) ระบุทรัพยากรที่บล็อกการเรนเดอร์ ห่วงโซ่การพึ่งพาเครือข่าย การเลื่อนเลย์เอาต์ ปัญหาแคช และช่องว่างด้านการเข้าถึง ใช้เมื่อถูกขอให้ตรวจสอบ โปรไฟล์ แก้ไขข้อบกพร่อง หรือปรับปรุงประสิทธิภาพการโหลดหน้าเว็บ คะแนน Lighthouse หรือความเร็วไซต์
wrangler
Cloudflare
Cloudflare Workers CLI สำหรับการปรับใช้ พัฒนา และจัดการ Workers, KV, R2, D1, Vectorize, Hyperdrive, Workers AI, Containers, Queues, Workflows, Pipelines และ Secrets Store โหลดก่อนรันคำสั่ง wrangler เพื่อให้แน่ใจว่าไวยากรณ์และแนวทางปฏิบัติที่ดีที่สุดถูกต้อง