Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Comprehensive Cloudflare platform skill covering Workers, D1, R2, KV, AI, Durable Objects, and security.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/snippets/gotchas.md
1# Gotchas & Best Practices23## Common Errors45### 1000: "Snippet execution failed"6Runtime error or syntax error. Wrap code in try/catch:7```javascript8try { return await fetch(request); }9catch (error) { return new Response(`Error: ${error.message}`, { status: 500 }); }10```1112### 1100: "Exceeded execution limit"13Code takes >5ms CPU. Simplify logic or move to Workers.1415### 1201: "Multiple origin fetches"16Call `fetch(request)` exactly once:17```javascript18// ❌ Multiple origin fetches19const r1 = await fetch(request); const r2 = await fetch(request);20// ✅ Single fetch, reuse response21const response = await fetch(request);22```2324### 1202: "Subrequest limit exceeded"25Pro: 2 subrequests, Business/Enterprise: 5. Reduce fetch calls.2627### "Cannot set property on immutable object"28Clone before modifying:29```javascript30const modifiedRequest = new Request(request);31modifiedRequest.headers.set("X-Custom", "value");32```3334### "caches is not defined"35Cache API NOT available in Snippets. Use Workers.3637### "Module not found"38Snippets don't support `import`. Use inline code or Workers.3940## Best Practices4142### Performance43- Keep code <10KB (32KB limit)44- Optimize for 5ms CPU45- Clone only when modifying46- Minimize subrequests4748### Security49- Validate all inputs50- Use Web Crypto API for hashing51- Sanitize headers before origin52- Don't log secrets5354### Debugging55```javascript56newResponse.headers.set("X-Debug-Country", request.cf.country);57```58```bash59curl -H "X-Test: true" https://example.com -v60```6162## Available APIs6364**✅ Available:** `fetch()`, `Request`, `Response`, `Headers`, `URL`, `crypto.subtle`, `crypto.randomUUID()`, `atob()`/`btoa()`, `JSON`6566**❌ NOT Available:** `caches`, `KV`, `D1`, `R2`, `Durable Objects`, `WebSocket`, `HTMLRewriter`, `import`, Node.js APIs6768## Limits6970| Resource | Limit |71|----------|-------|72| Snippet size | 32KB |73| Execution time | 5ms CPU |74| Subrequests (Pro/Biz) | 2/5 |75| Snippets/zone | 20 |7677## Performance Benchmarks7879| Operation | Time |80|-----------|------|81| Header set | <0.1ms |82| URL parsing | <0.2ms |83| fetch() | 1-3ms |84| SHA-256 | 0.5-1ms |8586**Migrate to Workers when:** >5ms needed, >5 subrequests, need storage (KV/D1/R2), need npm packages, >32KB code87