Your knowledge of Cloudflare Workers APIs, types, and configuration may be outdated. Prefer retrieval over pre-training for any Workers code task — writing or reviewing.
When to Use
Load this skill when:
- Writing new Cloudflare Workers code or scaffolding a new Worker project
- Reviewing existing Worker code in a PR, audit, or refactor
- Configuring
wrangler.jsonc / wrangler.toml — bindings, compatibility flags, observability
- Diagnosing common Workers anti-patterns: floating promises, global mutable state, unbounded
await response.text(), hardcoded secrets, REST API calls from inside a Worker
- Checking binding access patterns in platform base classes (
DurableObject, WorkerEntrypoint, Workflow)
Do not load this skill for:
- Durable Objects specifics — load the
durable-objects skill instead
- Wrangler CLI command reference — load the
wrangler skill
- Workflows rules — see Rules of Workflows
Prerequisites
- Node.js and npm installed on the host
wrangler available locally (node_modules/.bin/wrangler) or globally
- PowerShell as the primary shell on Windows host
- Internet access for fetching Cloudflare docs and
npm pack
Procedure
Step 1 — Fetch Latest References Before Writing or Reviewing
Before reviewing or writing Workers code, retrieve the current best practices page and relevant type definitions. If the project's node_modules has an older version, prefer the latest published version.
Fetch the canonical best practices page:
# Fetch the Workers best practices page
Invoke-WebRequest -Uri "https://developers.cloudflare.com/workers/best-practices/workers-best-practices/" -OutFile "$env:TEMP\workers-best-practices.html"
Fetch the latest @cloudflare/workers-types package:
# Create temp dir and fetch latest workers types
New-Item -ItemType Directory -Force -Path "$env:TEMP\workers-types-latest"
npm pack @cloudflare/workers-types --pack-destination "$env:TEMP\workers-types-latest"
# Extract the tarball
$tarball = Get-ChildItem "$env:TEMP\workers-types-latest\cloudflare-workers-types-*.tgz" | Select-Object -First 1
tar -xzf $tarball.FullName -C "$env:TEMP\workers-types-latest"
# Types at $env:TEMP\workers-types-latest\package\index.d.ts
Check the local wrangler config schema:
# If wrangler is installed locally, the schema is available at:
# node_modules\wrangler\config-schema.json
# Use it to validate config fields, binding shapes, and allowed values
Test-Path "node_modules\wrangler\config-schema.json"
Step 2 — Load Reference Files
Load these reference files at the right time during review:
| Reference file |
When to load |
references/rules.md |
Before flagging any anti-pattern — contains all best practice rules with correct code examples and anti-pattern side-by-side |
references/review.md |
During type validation and config validation steps — contains type validation steps, config validation, binding access patterns, and the full review process |
Step 3 — Retrieve Latest Docs for Any Uncertain API
If unsure about an API signature, config field, or binding shape, fetch the docs first:
# Search Cloudflare Workers docs
Invoke-WebRequest -Uri "https://developers.cloudflare.com/workers/" -UseBasicParsing
| Source |
How to retrieve |
Use for |
| Workers best practices |
Fetch https://developers.cloudflare.com/workers/best-practices/workers-best-practices/ |
Canonical rules, patterns, anti-patterns |
| Workers types |
See references/review.md for retrieval steps |
API signatures, handler types, binding types |
| Wrangler config schema |
node_modules/wrangler/config-schema.json |
Config fields, binding shapes, allowed values |
| Cloudflare docs |
Search tool or https://developers.cloudflare.com/workers/ |
API reference, compatibility dates/flags |
Step 4 — Review Workflow
Follow this ordered process for every Workers code review:
- Retrieve — fetch latest best practices page, workers types, and wrangler schema (Step 1 above)
- Read full files — not just diffs; context matters for binding access patterns
- Check types — binding access, handler signatures, no
any, no unsafe casts (see references/review.md)
- Check config —
compatibility_date, nodejs_compat, observability, secrets, binding-code consistency
- Check patterns — streaming, floating promises, global state, serialization boundaries
- Check security — crypto usage, secret handling, timing-safe comparisons, error handling
- Validate with tools —
npx tsc --noEmit, lint for no-floating-promises
- Reference rules — see
references/rules.md for each rule's correct pattern
Step 5 — Apply Configuration Rules
| Rule |
Summary |
How to verify |
| Compatibility date |
Set compatibility_date to today on new projects; update periodically on existing ones |
Check wrangler.jsonc for compatibility_date field |
| nodejs_compat |
Enable the nodejs_compat flag — many libraries depend on Node.js built-ins |
Check compatibility_flags array in config |
| wrangler types |
Run wrangler types to generate Env — never hand-write binding interfaces |
Run npx wrangler types and check for generated worker-configuration.d.ts |
| Secrets |
Use wrangler secret put, never hardcode secrets in config or source |
Search source for YOUR_KEY-style literals; check for wrangler secret usage |
| wrangler.jsonc |
Use JSONC config for non-secret settings — newer features are JSON-only |
Verify config file is wrangler.jsonc not wrangler.toml |
Step 6 — Apply Request & Response Handling Rules
| Rule |
Summary |
| Streaming |
Stream large/unknown payloads — never await response.text() on unbounded data |
| waitUntil |
Use ctx.waitUntil() for post-response work; do not destructure ctx |
Step 7 — Apply Architecture Rules
| Rule |
Summary |
| Bindings over REST |
Use in-process bindings (KV, R2, D1, Queues) — not the Cloudflare REST API |
| Queues & Workflows |
Move async/background work off the critical path |
| Service bindings |
Use service bindings for Worker-to-Worker calls — not public HTTP |
| Hyperdrive |
Always use Hyperdrive for external PostgreSQL/MySQL connections |
Step 8 — Apply Observability Rules
| Rule |
Summary |
| Logs & Traces |
Enable observability in config with head_sampling_rate; use structured JSON logging |
Step 9 — Apply Code Pattern Rules
| Rule |
Summary |
| No global request state |
Never store request-scoped data in module-level variables |
| Floating promises |
Every Promise must be awaited, returned, voided, or passed to ctx.waitUntil() |
Step 10 — Apply Security Rules
| Rule |
Summary |
| Web Crypto |
Use crypto.randomUUID() / crypto.getRandomValues() — never Math.random() for security |
| No passThroughOnException |
Use explicit try/catch with structured error responses |
Pitfalls
Anti-Patterns to Flag
| Anti-pattern |
Why it matters |
await response.text() on unbounded data |
Memory exhaustion — 128 MB limit |
| Hardcoded secrets in source or config |
Credential leak via version control |
Math.random() for tokens/IDs |
Predictable, not cryptographically secure |
Bare fetch() without await or waitUntil |
Floating promise — dropped result, swallowed error |
| Module-level mutable variables for request state |
Cross-request data leaks, stale state, I/O errors |
| Cloudflare REST API from inside a Worker |
Unnecessary network hop, auth overhead, added latency |
ctx.passThroughOnException() as error handling |
Hides bugs, makes debugging impossible |
Hand-written Env interface |
Drifts from actual wrangler config bindings |
| Direct string comparison for secret values |
Timing side-channel — use crypto.subtle.timingSafeEqual |
Destructuring ctx (const { waitUntil } = ctx) |
Loses this binding — throws "Illegal invocation" at runtime |
any on Env or handler params |
Defeats type safety for all binding access |
as unknown as T double-cast |
Hides real type incompatibilities — fix the design |
implements on platform base classes (instead of extends) |
Legacy — loses this.ctx, this.env. Applies to DurableObject, WorkerEntrypoint, Workflow |
env.X inside platform base class |
Should be this.env.X in classes extending DurableObject, WorkerEntrypoint, etc. |
Hard Rules
- Never hardcode secrets in source or config — always use
wrangler secret put
- Never use
Math.random() for security-sensitive tokens or IDs
- Never store request-scoped data in module-level mutable variables
- Never destructure
ctx — always call ctx.waitUntil() directly
- Never hand-write the
Env interface — run wrangler types to generate it
- Never use
any on Env or handler parameters
- Never use
as unknown as T double-cast — fix the underlying type incompatibility
- Never use
implements on platform base classes — use extends instead
- Never access
env.X inside a platform base class — use this.env.X
- Never use
ctx.passThroughOnException() as error handling — use explicit try/catch
- Never call the Cloudflare REST API from inside a Worker — use in-process bindings
- Never
await response.text() on unbounded data — stream instead
Verification
Type Check
npx tsc --noEmit
Expected: zero errors. If errors appear, check binding access patterns and handler signatures against references/review.md.
Lint for Floating Promises
# If using TypeScript ESLint with strict config
npx eslint . --rule '{"no-floating-promises": "error"}'
Expected: zero no-floating-promises violations.
Generate Types from Wrangler Config
npx wrangler types
Expected: generates worker-configuration.d.ts (or project-configured output). Verify the generated Env interface matches bindings declared in wrangler.jsonc.
Check Config for Required Fields
# Verify wrangler.jsonc has compatibility_date, nodejs_compat, and observability
Get-Content "wrangler.jsonc" | Select-String -Pattern "compatibility_date|nodejs_compat|observability|head_sampling_rate"
Expected: all four patterns found in the config file.
Check for Hardcoded Secrets
# Search for common secret patterns in source (excluding node_modules)
Get-ChildItem -Recurse -Include "*.ts","*.js","*.jsonc","*.toml" -Exclude "node_modules" | Select-String -Pattern "(?i)(api_key|secret|token|password)\s*[:=]\s*['\""][^'\""]{8,}" | Where-Object { $_.Path -notmatch "node_modules" }
Expected: zero matches (or only placeholder values like YOUR_KEY).
Principles
- Be certain. Retrieve before flagging. If unsure about an API, config field, or pattern, fetch the docs first.
- Provide evidence. Reference line numbers, tool output, or docs links.
- Focus on what developers will copy. Workers code in examples and docs gets pasted into production.
- Correctness over completeness. A concise example that works beats a comprehensive one with errors.
Related Skills
- Durable Objects: load the
durable-objects skill for DO-specific patterns
- Workflows: see Rules of Workflows
- Wrangler CLI commands: load the
wrangler skill
1---
2name: workers-best-practices
3description: "Reviews and authors Cloudflare Workers against current best practices (streaming, floating promises, global state, secrets, wrangler.jsonc, bindings). Use when writing or reviewing Workers or wrangler config. Not for Durable Objects details (durable-objects), Wrangler CLI reference (wrangler), or Cloudflare Workflows rules."
4version: 1.0.1
5metadata:
6 source: https://skillmd.com/skills/kayforkind/workers-best-practices
7---
8
9Your knowledge of Cloudflare Workers APIs, types, and configuration may be outdated. **Prefer retrieval over pre-training** for any Workers code task — writing or reviewing.
10
11## When to Use
12
13Load this skill when:
14- Writing new Cloudflare Workers code or scaffolding a new Worker project
15- Reviewing existing Worker code in a PR, audit, or refactor
16- Configuring `wrangler.jsonc` / `wrangler.toml` — bindings, compatibility flags, observability
17- Diagnosing common Workers anti-patterns: floating promises, global mutable state, unbounded `await response.text()`, hardcoded secrets, REST API calls from inside a Worker
18- Checking binding access patterns in platform base classes (`DurableObject`, `WorkerEntrypoint`, `Workflow`)
19
20Do **not** load this skill for:
21- Durable Objects specifics — load the `durable-objects` skill instead
22- Wrangler CLI command reference — load the `wrangler` skill
23- Workflows rules — see [Rules of Workflows](https://developers.cloudflare.com/workflows/build/rules-of-workflows/)
24
25## Prerequisites
26
27- Node.js and npm installed on the host
28- `wrangler` available locally (`node_modules/.bin/wrangler`) or globally
29- PowerShell as the primary shell on Windows host
30- Internet access for fetching Cloudflare docs and `npm pack`
31
32## Procedure
33
34### Step 1 — Fetch Latest References Before Writing or Reviewing
35
36Before reviewing or writing Workers code, retrieve the current best practices page and relevant type definitions. If the project's `node_modules` has an older version, **prefer the latest published version**.
37
38**Fetch the canonical best practices page:**
39
40```powershell
41# Fetch the Workers best practices page
42Invoke-WebRequest -Uri "https://developers.cloudflare.com/workers/best-practices/workers-best-practices/" -OutFile "$env:TEMP\workers-best-practices.html"
43```
44
45**Fetch the latest `@cloudflare/workers-types` package:**
46
47```powershell
48# Create temp dir and fetch latest workers types
49New-Item -ItemType Directory -Force -Path "$env:TEMP\workers-types-latest"
50npm pack @cloudflare/workers-types --pack-destination "$env:TEMP\workers-types-latest"
51# Extract the tarball
52$tarball = Get-ChildItem "$env:TEMP\workers-types-latest\cloudflare-workers-types-*.tgz" | Select-Object -First 1
53tar -xzf $tarball.FullName -C "$env:TEMP\workers-types-latest"
54# Types at $env:TEMP\workers-types-latest\package\index.d.ts
55```
56
57**Check the local wrangler config schema:**
58
59```powershell
60# If wrangler is installed locally, the schema is available at:
61# node_modules\wrangler\config-schema.json
62# Use it to validate config fields, binding shapes, and allowed values
63Test-Path "node_modules\wrangler\config-schema.json"
64```
65
66### Step 2 — Load Reference Files
67
68Load these reference files at the right time during review:
69
70| Reference file | When to load |
71|---------------|--------------|
72| `references/rules.md` | Before flagging any anti-pattern — contains all best practice rules with correct code examples and anti-pattern side-by-side |
73| `references/review.md` | During type validation and config validation steps — contains type validation steps, config validation, binding access patterns, and the full review process |
74
75### Step 3 — Retrieve Latest Docs for Any Uncertain API
76
77If unsure about an API signature, config field, or binding shape, fetch the docs first:
78
79```powershell
80# Search Cloudflare Workers docs
81Invoke-WebRequest -Uri "https://developers.cloudflare.com/workers/" -UseBasicParsing
82```
83
84| Source | How to retrieve | Use for |
85|--------|----------------|---------|
86| Workers best practices | Fetch `https://developers.cloudflare.com/workers/best-practices/workers-best-practices/` | Canonical rules, patterns, anti-patterns |
87| Workers types | See `references/review.md` for retrieval steps | API signatures, handler types, binding types |
88| Wrangler config schema | `node_modules/wrangler/config-schema.json` | Config fields, binding shapes, allowed values |
89| Cloudflare docs | Search tool or `https://developers.cloudflare.com/workers/` | API reference, compatibility dates/flags |
90
91### Step 4 — Review Workflow
92
93Follow this ordered process for every Workers code review:
94
951. **Retrieve** — fetch latest best practices page, workers types, and wrangler schema (Step 1 above)
962. **Read full files** — not just diffs; context matters for binding access patterns
973. **Check types** — binding access, handler signatures, no `any`, no unsafe casts (see `references/review.md`)
984. **Check config** — `compatibility_date`, `nodejs_compat`, `observability`, secrets, binding-code consistency
995. **Check patterns** — streaming, floating promises, global state, serialization boundaries
1006. **Check security** — crypto usage, secret handling, timing-safe comparisons, error handling
1017. **Validate with tools** — `npx tsc --noEmit`, lint for `no-floating-promises`
1028. **Reference rules** — see `references/rules.md` for each rule's correct pattern
103
104### Step 5 — Apply Configuration Rules
105
106| Rule | Summary | How to verify |
107|------|---------|---------------|
108| Compatibility date | Set `compatibility_date` to today on new projects; update periodically on existing ones | Check `wrangler.jsonc` for `compatibility_date` field |
109| nodejs_compat | Enable the `nodejs_compat` flag — many libraries depend on Node.js built-ins | Check `compatibility_flags` array in config |
110| wrangler types | Run `wrangler types` to generate `Env` — never hand-write binding interfaces | Run `npx wrangler types` and check for generated `worker-configuration.d.ts` |
111| Secrets | Use `wrangler secret put`, never hardcode secrets in config or source | Search source for `YOUR_KEY`-style literals; check for `wrangler secret` usage |
112| wrangler.jsonc | Use JSONC config for non-secret settings — newer features are JSON-only | Verify config file is `wrangler.jsonc` not `wrangler.toml` |
113
114### Step 6 — Apply Request & Response Handling Rules
115
116| Rule | Summary |
117|------|---------|
118| Streaming | Stream large/unknown payloads — never `await response.text()` on unbounded data |
119| waitUntil | Use `ctx.waitUntil()` for post-response work; do not destructure `ctx` |
120
121### Step 7 — Apply Architecture Rules
122
123| Rule | Summary |
124|------|---------|
125| Bindings over REST | Use in-process bindings (KV, R2, D1, Queues) — not the Cloudflare REST API |
126| Queues & Workflows | Move async/background work off the critical path |
127| Service bindings | Use service bindings for Worker-to-Worker calls — not public HTTP |
128| Hyperdrive | Always use Hyperdrive for external PostgreSQL/MySQL connections |
129
130### Step 8 — Apply Observability Rules
131
132| Rule | Summary |
133|------|---------|
134| Logs & Traces | Enable `observability` in config with `head_sampling_rate`; use structured JSON logging |
135
136### Step 9 — Apply Code Pattern Rules
137
138| Rule | Summary |
139|------|---------|
140| No global request state | Never store request-scoped data in module-level variables |
141| Floating promises | Every Promise must be `await`ed, `return`ed, `void`ed, or passed to `ctx.waitUntil()` |
142
143### Step 10 — Apply Security Rules
144
145| Rule | Summary |
146|------|---------|
147| Web Crypto | Use `crypto.randomUUID()` / `crypto.getRandomValues()` — never `Math.random()` for security |
148| No passThroughOnException | Use explicit try/catch with structured error responses |
149
150## Pitfalls
151
152### Anti-Patterns to Flag
153
154| Anti-pattern | Why it matters |
155|-------------|----------------|
156| `await response.text()` on unbounded data | Memory exhaustion — 128 MB limit |
157| Hardcoded secrets in source or config | Credential leak via version control |
158| `Math.random()` for tokens/IDs | Predictable, not cryptographically secure |
159| Bare `fetch()` without `await` or `waitUntil` | Floating promise — dropped result, swallowed error |
160| Module-level mutable variables for request state | Cross-request data leaks, stale state, I/O errors |
161| Cloudflare REST API from inside a Worker | Unnecessary network hop, auth overhead, added latency |
162| `ctx.passThroughOnException()` as error handling | Hides bugs, makes debugging impossible |
163| Hand-written `Env` interface | Drifts from actual wrangler config bindings |
164| Direct string comparison for secret values | Timing side-channel — use `crypto.subtle.timingSafeEqual` |
165| Destructuring `ctx` (`const { waitUntil } = ctx`) | Loses `this` binding — throws "Illegal invocation" at runtime |
166| `any` on `Env` or handler params | Defeats type safety for all binding access |
167| `as unknown as T` double-cast | Hides real type incompatibilities — fix the design |
168| `implements` on platform base classes (instead of `extends`) | Legacy — loses `this.ctx`, `this.env`. Applies to DurableObject, WorkerEntrypoint, Workflow |
169| `env.X` inside platform base class | Should be `this.env.X` in classes extending DurableObject, WorkerEntrypoint, etc. |
170
171### Hard Rules
172
173- **Never** hardcode secrets in source or config — always use `wrangler secret put`
174- **Never** use `Math.random()` for security-sensitive tokens or IDs
175- **Never** store request-scoped data in module-level mutable variables
176- **Never** destructure `ctx` — always call `ctx.waitUntil()` directly
177- **Never** hand-write the `Env` interface — run `wrangler types` to generate it
178- **Never** use `any` on `Env` or handler parameters
179- **Never** use `as unknown as T` double-cast — fix the underlying type incompatibility
180- **Never** use `implements` on platform base classes — use `extends` instead
181- **Never** access `env.X` inside a platform base class — use `this.env.X`
182- **Never** use `ctx.passThroughOnException()` as error handling — use explicit try/catch
183- **Never** call the Cloudflare REST API from inside a Worker — use in-process bindings
184- **Never** `await response.text()` on unbounded data — stream instead
185
186## Verification
187
188### Type Check
189
190```powershell
191npx tsc --noEmit
192```
193
194Expected: zero errors. If errors appear, check binding access patterns and handler signatures against `references/review.md`.
195
196### Lint for Floating Promises
197
198```powershell
199# If using TypeScript ESLint with strict config
200npx eslint . --rule '{"no-floating-promises": "error"}'
201```
202
203Expected: zero `no-floating-promises` violations.
204
205### Generate Types from Wrangler Config
206
207```powershell
208npx wrangler types
209```
210
211Expected: generates `worker-configuration.d.ts` (or project-configured output). Verify the generated `Env` interface matches bindings declared in `wrangler.jsonc`.
212
213### Check Config for Required Fields
214
215```powershell
216# Verify wrangler.jsonc has compatibility_date, nodejs_compat, and observability
217Get-Content "wrangler.jsonc" | Select-String -Pattern "compatibility_date|nodejs_compat|observability|head_sampling_rate"
218```
219
220Expected: all four patterns found in the config file.
221
222### Check for Hardcoded Secrets
223
224```powershell
225# Search for common secret patterns in source (excluding node_modules)
226Get-ChildItem -Recurse -Include "*.ts","*.js","*.jsonc","*.toml" -Exclude "node_modules" | Select-String -Pattern "(?i)(api_key|secret|token|password)\s*[:=]\s*['\""][^'\""]{8,}" | Where-Object { $_.Path -notmatch "node_modules" }
227```
228
229Expected: zero matches (or only placeholder values like `YOUR_KEY`).
230
231## Principles
232
233- **Be certain.** Retrieve before flagging. If unsure about an API, config field, or pattern, fetch the docs first.
234- **Provide evidence.** Reference line numbers, tool output, or docs links.
235- **Focus on what developers will copy.** Workers code in examples and docs gets pasted into production.
236- **Correctness over completeness.** A concise example that works beats a comprehensive one with errors.
237
238## Related Skills
239
240- **Durable Objects**: load the `durable-objects` skill for DO-specific patterns
241- **Workflows**: see [Rules of Workflows](https://developers.cloudflare.com/workflows/build/rules-of-workflows/)
242- **Wrangler CLI commands**: load the `wrangler` skill