mcpField
W
workers-best-practices

workers-best-practices

github.com/tejas-kaleCloud & DevOps038 installsUpdated 3d ago
Repository

About workers-best-practices

workers-best-practices is a skill for authors and reviewers of Cloudflare Workers code. It requires fetching current documentation, types, and configuration schema before applying its rules and reporting evidence-backed findings.

Key features

  1. 01Fetch current Workers docs and types
  2. 02Apply authoring and review rules
  3. 03Check TypeScript and promise lint
  4. 04Report findings with evidence and severity

Use cases

  1. 01Authoring code against current Workers APIs
  2. 02Reviewing Worker files for anti-patterns
  3. 03Validating types, bindings, and configuration

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/tejas-kale/skills --skill workers-best-practices

After installing, type/workers-best-practicesin Claude Code to confirm it loaded.

Bundled filesreferences/review.mdreferences/rules.md

Details

From the project README

Workers Best Practices

Your baked-in knowledge of Workers APIs, config fields, and binding shapes is stale. Retrieve first: every API signature, config field, and binding shape you assert comes from a source fetched this session, and a finding you cannot cite a source for does not get reported.

1. Retrieve

Fetch before writing or reviewing a single line. Prefer the latest published version over whatever node_modules pins.

# Latest workers types → /tmp/workers-types-latest/package/index.d.ts
mkdir -p /tmp/workers-types-latest && \
  npm pack @cloudflare/workers-types --pack-destination /tmp/workers-types-latest && \
  tar -xzf /tmp/workers-types-latest/cloudflare-workers-types-*.tgz -C /tmp/workers-types-latest
SourceWhereUse for
Best practices pagehttps://developers.cloudflare.com/workers/best-practices/workers-best-practices/Canonical rules and anti-patterns
Workers typesthe npm pack aboveAPI signatures, handler types, binding types
Wrangler config schemanode_modules/wrangler/config-schema.jsonConfig fields, binding shapes, allowed values
Cloudflare docssearch, or https://developers.cloudflare.com/workers/API reference, compatibility dates and flags

Done when the types, the config schema, and the best practices page are all in context.

2. Apply the rules

references/rules.md is the single source of truth for every rule — configuration, request handling, architecture, observability, code patterns, security, testing — each with its correct pattern and the anti-pattern it replaces. Read it, then work the rule set:

  • Authoring — every rule in the relevant sections holds in the code you write.
  • Reviewing — read the full files rather than the diff, since binding access and global state only read correctly in context, then check every rule against them.

Done when every rule in references/rules.md has been applied or consciously judged inapplicable — not when the obvious ones pass.

3. Verify and report

Run npx tsc --noEmit and lint for no-floating-promises; a rule a tool can check is checked by the tool, not by eye.

For a review, references/review.md carries the review-specific reference: type and config validation, serialization boundaries, how to grade illustrative vs executable code, risk levels, and the finding format. Read it before writing up findings.

Done when every finding names a file and line, cites its evidence, and carries a severity.

Anti-patterns to flag

The recurring ones, as a scan list — each is explained in full in references/rules.md.

Anti-patternWhy it matters
await response.text() on unbounded dataMemory exhaustion — 128 MB limit
Hardcoded secrets in source or configCredential leak via version control
Math.random() for tokens or IDsPredictable, not cryptographically secure
Bare fetch() without await or waitUntilFloating promise — dropped result, swallowed error
Module-level mutable variables for request stateCross-request data leaks, stale state, I/O errors
Cloudflare REST API from inside a WorkerExtra network hop, auth overhead, added latency
ctx.passThroughOnException() as error handlingHides bugs and makes debugging impossible
Hand-written Env interfaceDrifts from the actual wrangler config bindings
Direct string comparison of secretsTiming side-channel — use crypto.subtle.timingSafeEqual
Destructuring ctx (const { waitUntil } = ctx)Loses this binding — "Illegal invocation" at runtime
any on Env or handler paramsDefeats type safety for every binding access
as unknown as T double-castHides a real type incompatibility — fix the design
implements on a platform base classLegacy — loses this.ctx and this.env; use extends on DurableObject, WorkerEntrypoint, Workflow
env.X inside a platform base classShould be this.env.X

Scope

Workers themselves. For the wider platform — Durable Objects, Workflows, Wrangler commands, KV/D1/R2 — load the cloudflare umbrella skill, whose references/ carries a file per product.

Comments

Markdown supported