workers-best-practices

Reviews and authors Cloudflare Workers code against current production best practices.

Updated Nov 23, 2024
One-click install
npx skills add https://github.com/tokisakiyuu/dotfiles --skill workers-best-practices-tokisakiyuu
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: workers-best-practices
Source: https://github.com/tokisakiyuu/dotfiles/tree/main/home/dot_claude/skills/workers-best-practices
Command: npx skills add https://github.com/tokisakiyuu/dotfiles --skill workers-best-practices-tokisakiyuu

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Cloudflare Workers APIs, types, and wrangler configuration evolve quickly, so code written from stale knowledge often contains anti-patterns like floating promises, global request state, hardcoded secrets, or outdated binding access that cause runtime failures and security issues. ## Core Features & Use Cases - Retrieval-first review: Fetches the latest Workers best practices page, @cloudflare/workers-types, and the wrangler config schema before writing or reviewing code, instead of relying on pre-trained knowledge. - Rule-based checks: Covers configuration (compatibility_date, nodejs_compat, secrets), streaming, waitUntil, bindings over REST APIs, observability, Web Crypto security, and serialization boundaries. - Structured review output: Flags anti-patterns with severity levels (CRITICAL/HIGH/MEDIUM/LOW), file and line evidence, and suggested fixes. - Use Case: When reviewing a pull request that adds a new Worker with KV and Queue bindings, load this skill to verify binding-code consistency, catch a missing nodejs_compat flag, and flag an unawaited fetch call before merge. ## Quick Start Ask the assistant to review your Worker code and wrangler.jsonc against the latest Cloudflare Workers best practices.

Frequently Asked Questions about workers-best-practices

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I review Cloudflare Workers code for best practices?▼

Retrieve the latest Workers best practices page, workers types, and wrangler schema first, then check types, config, patterns, and security in order. Validate with npx tsc --noEmit and a no-floating-promises lint rule, and report findings with severity levels and line references.

What are common Cloudflare Workers anti-patterns to avoid?▼

Common anti-patterns include awaiting response.text() on unbounded data, hardcoding secrets in wrangler config, using Math.random() for tokens, floating promises without await or waitUntil, module-level mutable request state, and calling the Cloudflare REST API instead of bindings.

Should I use wrangler.toml or wrangler.jsonc for Workers config?▼

Use wrangler.jsonc for new projects because newer Workers features are JSON-only and JSONC supports comments. wrangler.toml is legacy and acceptable in existing projects but should be flagged in new ones.

How do I access bindings in a Cloudflare Worker?▼

In module export handlers like fetch, access bindings via the env parameter (env.X). In classes extending platform base classes such as DurableObject, WorkerEntrypoint, or Workflow, use this.env.X instead. Mixing these patterns is the most common binding error.

Why does destructuring ctx fail in a Cloudflare Worker?▼

Destructuring ctx (const { waitUntil } = ctx) loses the this binding and throws an Illegal invocation error at runtime. Always call ctx.waitUntil() directly on the ExecutionContext object passed to your handler.

When should I use Hyperdrive for database connections in Workers?▼

Use Hyperdrive whenever a Worker connects to an external PostgreSQL or MySQL database. It maintains a regional connection pool that eliminates per-request TCP, TLS, and auth overhead, and requires the nodejs_compat compatibility flag.