All skills
cloudflare avatar

/workers-best-practices

@41e0d19 official
by cloudflarecloudflare/skills3k stars
298

Cloudflare Workers best practices for production applications. Use when writing, reviewing, or configuring Workers.

Use this Skill: https://skilld.dev/gh/cloudflare/skills/workers-best-practices

This session only. Nothing lands on disk.

referencesconfiguration.md

≈1.4k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Workers Configuration and Observability

Use the project's Wrangler configuration and installed node_modules/wrangler/config-schema.json to check fields and binding declarations. Consult current product docs when a field or compatibility requirement needs verification. Doc paths below are relative to https://developers.cloudflare.com.

  • Configuration: compatibility dates, Node.js compatibility, generated types, secrets, and config format
  • Binding consistency: configuration and code agree
  • Observability: enable logs and traces, configure sampling, and emit structured logs

Configuration

Keep compatibility_date current

Set compatibility_date to today on new projects. Encourage periodic updates on existing projects to adopt new runtime behavior and fixes. Review the intervening compatibility changes and run relevant tests when advancing the date.

Check: compatibility_date exists and supports the affected feature with the configured flags. Recommend updates as maintenance; flag a compatibility defect when the configured date or flags do not support the required behavior.

// wrangler.jsonc
{
  "compatibility_date": "$today",  // Replace with today's date (YYYY-MM-DD)
  "compatibility_flags": ["nodejs_compat"]
}

Retrieve: current compatibility dates at /workers/configuration/compatibility-dates/.

Enable nodejs_compat

The nodejs_compat flag enables Node.js built-in modules (node:crypto, node:buffer, node:stream). Many libraries require it. Missing this flag causes cryptic import errors at runtime.

Check: compatibility_flags includes "nodejs_compat".

{
  "compatibility_flags": ["nodejs_compat"]
}

Generate binding types with wrangler types

Never hand-write the Env interface. Run wrangler types to generate it from the wrangler config. Re-run after adding or renaming any binding.

Check: no manually defined Env or interface Env that duplicates wrangler config bindings. Look for satisfies ExportedHandler<Env> pattern on the default export.

// Generated by wrangler types — always matches actual config
export default {
  async fetch(request: Request, env: Env): Promise<Response> {
    const value = await env.MY_KV.get("key");
    return new Response(value);
  },
} satisfies ExportedHandler<Env>;

Anti-pattern:

// Hand-written Env that drifts from actual bindings
interface Env {
  MY_KV: KVNamespace;  // What if the binding name changed?
}

Store secrets with wrangler secret

Secrets must never appear in wrangler config or source code. Use wrangler secret put and access via env at runtime. Non-secret config goes in vars.

Check: no string literals that look like API keys, tokens, or credentials. Verify .env is in .gitignore for local dev.

{
  "vars": {
    "API_BASE_URL": "https://api.example.com"  // Non-secret: OK in config
  }
  // Secrets set via: wrangler secret put API_KEY
}

Anti-pattern:

{
  "vars": {
    "API_KEY": "sk-live-abc123..."  // Secret in version control
  }
}

Use wrangler.jsonc for config

Prefer wrangler.jsonc over wrangler.toml. Newer features are JSON-only. JSONC supports comments for documenting config decisions.

Check: project uses wrangler.jsonc (or wrangler.json). Flag wrangler.toml in new projects.


Binding-code consistency

For executable Worker examples, verify name, compatibility_date, and main against the target Wrangler schema.

  1. Every env.X reference in code has a corresponding binding declaration in config
  2. Names match exactly (case-sensitive)
  3. For Durable Objects: class_name matches the exported class name

An unused binding alone is not a finding; establish a concrete configuration or runtime consequence before recommending a change.

For a new Durable Object class, verify its migration entry and exported class name against the target Wrangler schema.

Observability

Enable Workers Logs and Traces

Enable Workers Logs and Traces in Wrangler config before deploying to production. Set observability.enabled and observability.traces.enabled to true; the top-level setting alone does not enable traces. Use head_sampling_rate to control volume and cost. Use structured JSON logging — console.log(JSON.stringify({...})) — so logs are searchable. Use console.error for errors (appears at error severity in the dashboard).

Check: logs and traces are enabled in the target deployment environment, with neither disabled by an environment override. Check observability.enabled, observability.logs.enabled, and observability.traces.enabled, accounting for their defaults. Logging uses structured JSON, not string concatenation.

{
  "observability": {
    "enabled": true,
    "logs": { "enabled": true, "head_sampling_rate": 1 },
    "traces": { "enabled": true, "head_sampling_rate": 0.01 }
  }
}
// Structured JSON — searchable and filterable
console.log(JSON.stringify({ message: "incoming request", method: request.method, path: url.pathname }));

// Error severity
console.error(JSON.stringify({ message: "request failed", error: e instanceof Error ? e.message : String(e) }));

Anti-pattern:

// Unstructured string logs — hard to query
console.log("Got a request to " + url.pathname);

Retrieve: Workers Logs and Traces for current config options.

Source: SKILL.md on GitHub

1 warning16d5 checks · Risk SAFE
  • Gen Agent Trust Hub16d

    This skill provides security and performance best practices for developing Cloudflare Workers. It contains no malicious patterns and actively encourages secure coding habits such as secret management and cryptographically secure random number generation.

  • Socket16d

    No alerts

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer7mo

    3/3 files flagged

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 41e0d19. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 5 hours ago.

Activeupdated 6 hours ago
  • Security
  • cloudflare
  • workers
  • best-practices
  • wrangler
  • observability
  • streaming
  • bindings
  • durable-objects
  • code-review

README badge

README badge for cloudflare/skills/workers-best-practices

Reviews and authors Cloudflare Workers code against production best practices, including streaming, floating promises, global state, secrets, bindings, and observability. Fetches latest Workers types and config schema rather than relying on pre-trained knowledge, making it suitable for code review and new Worker development in wrangler projects.

Generated from the current SKILL.md.

Does this skill cover Durable Objects and Workflows?
No. This skill focuses on Workers-specific best practices. Load the separate durable-objects skill for Durable Objects guidance, and refer to the Rules of Workflows documentation for Workflows.
Should I use pre-trained knowledge or fetch fresh docs?
Always fetch fresh docs. The skill is designed to retrieve the latest Workers best practices page, types, and wrangler schema before writing or reviewing code, because APIs and config fields change frequently.
What anti-patterns does this skill flag?
Common patterns like unbounded `await response.text()` calls, hardcoded secrets, floating promises, module-level request state, destructuring ctx, and using the Cloudflare REST API from inside a Worker instead of in-process bindings.
Does this skill validate TypeScript types and config?
Yes. The skill checks binding types, handler signatures, wrangler.jsonc config fields, and will flag unsafe patterns like bare `any` types, double-casts, and hand-written Env interfaces that drift from actual bindings.
Can this skill help me set up observability and logging?
Yes. The skill covers enabling observability in wrangler config with head_sampling_rate and recommends structured JSON logging patterns for production Workers.

Generated from the current SKILL.md. These answers refresh after source changes.