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