workers-best-practices

द्वारा Cloudflare

Cloudflare Workers कोड की समीक्षा करता है और उसे उत्पादन सर्वोत्तम प्रथाओं के अनुसार लिखता है। नए Workers लिखते समय, Worker कोड की समीक्षा करते समय, wrangler.jsonc कॉन्फ़िगर करते समय, या सामान्य Workers एंटी-पैटर्न (स्ट्रीमिंग, फ्लोटिंग प्रॉमिसेस, ग्लोबल स्टेट, सीक्रेट्स, बाइंडिंग्स, ऑब्जर्वेबिलिटी) की जाँच करते समय लोड करें। पूर्व-प्रशिक्षित ज्ञान की तुलना में 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

Cloudflare की और Skills

agents-sdk
Cloudflare
Cloudflare Workers पर Agents SDK का उपयोग करके AI एजेंट बनाएं। स्टेटफुल एजेंट, ड्यूरेबल वर्कफ़्लो, रीयल-टाइम WebSocket ऐप, शेड्यूल किए गए कार्य, MCP सर्वर या चैट एप्लिकेशन बनाते समय लोड करें। इसमें Agent क्लास, स्टेट प्रबंधन, कॉल करने योग्य RPC, Workflows एकीकरण और React हुक शामिल हैं।
building-ai-agent-on-cloudflare
Cloudflare
Cloudflare पर Agents SDK का उपयोग करके AI एजेंट बनाता है, जिसमें स्थिति प्रबंधन, रीयल-टाइम WebSockets, निर्धारित कार्य, उपकरण एकीकरण और चैट क्षमताएं शामिल हैं। Workers पर तैनात करने के लिए उत्पादन-तैयार एजेंट कोड उत्पन्न करता है। उपयोग करें जब: उपयोगकर्ता "एजेंट बनाएं", "AI एजेंट", "चैट एजेंट", "स्थिति-युक्त एजेंट" चाहता है, "Agents SDK" का उल्लेख करता है, "रीयल-टाइम AI", "WebSocket AI" की आवश्यकता है,
development
building-mcp-server-on-cloudflare
Cloudflare
Cloudflare Workers पर रिमोट MCP (मॉडल कॉन्टेक्स्ट प्रोटोकॉल) सर्वर बनाता है, जिसमें टूल्स, OAuth प्रमाणीकरण और प्रोडक्शन डिप्लॉयमेंट शामिल हैं। सर्वर कोड जनरेट करता है, प्रमाणीकरण प्रदाताओं को कॉन्फ़िगर करता है और Workers पर डिप्लॉय करता है। उपयोग तब करें जब: उपयोगकर्ता "MCP सर्वर बनाएं", "MCP टूल्स बनाएं", "रिमोट MCP", "MCP डिप्लॉय करें", "MCP में OAuth जोड़ें" या Cloudflare पर मॉडल कॉन्टेक्स्ट प्रोटो
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 सिस्टम, इंटरैक्टिव डेव एनवायरनमेंट या अविश्वसनीय कोड निष्पादित करते समय लोड करें। इसमें सैंडबॉक्स SDK जीवनचक्र, कमांड, फाइलें, कोड इंटरप्रेटर और पूर्वावलोकन URL शामिल हैं।
web-perf
Cloudflare
क्रोम DevTools MCP का उपयोग करके वेब प्रदर्शन का विश्लेषण करता है। कोर वेब वाइटल्स (FCP, LCP, TBT, CLS, स्पीड इंडेक्स) को मापता है, रेंडर-ब्लॉकिंग संसाधनों, नेटवर्क निर्भरता श्रृंखलाओं, लेआउट शिफ्ट्स, कैशिंग समस्याओं और पहुंच संबंधी अंतरालों की पहचान करता है। जब पेज लोड प्रदर्शन, Lighthouse स्कोर या साइट स्पीड का ऑडिट, प्रोफाइल, डीबग या ऑप्टिमाइज़ करने के लिए क
wrangler
Cloudflare
Cloudflare Workers CLI जो Workers, KV, R2, D1, Vectorize, Hyperdrive, Workers AI, Containers, Queues, Workflows, Pipelines और Secrets Store को डिप्लॉय, डेवलप और प्रबंधित करने के लिए है। wrangler कमांड चलाने से पहले लोड करें ताकि सही सिंटैक्स और सर्वोत्तम प्रथाओं को सुनिश्चित किया जा सके।