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/README.md
1# Cloudflare Snippets Skill Reference23## Description4Expert guidance for **Cloudflare Snippets ONLY** - a lightweight JavaScript-based edge logic platform for modifying HTTP requests and responses. Snippets run as part of the Ruleset Engine and are included at no additional cost on paid plans (Pro, Business, Enterprise).56## What Are Snippets?7Snippets are JavaScript functions executed at the edge as part of Cloudflare's Ruleset Engine. Key characteristics:8- **Execution time**: 5ms CPU limit per request9- **Size limit**: 32KB per snippet10- **Runtime**: V8 isolate (subset of Workers APIs)11- **Subrequests**: 2-5 fetch calls depending on plan12- **Cost**: Included with Pro/Business/Enterprise plans1314## Snippets vs Workers Decision Matrix1516| Factor | Choose Snippets If... | Choose Workers If... |17|--------|----------------------|---------------------|18| **Complexity** | Simple request/response modifications | Complex business logic, routing, middleware |19| **Execution time** | <5ms sufficient | Need >5ms or variable time |20| **Subrequests** | 2-5 fetch calls sufficient | Need >5 subrequests or complex orchestration |21| **Code size** | <32KB sufficient | Need >32KB or npm dependencies |22| **Cost** | Want zero additional cost | Can afford $5/mo + usage |23| **APIs** | Need basic fetch, headers, URL | Need KV, D1, R2, Durable Objects, cron triggers |24| **Deployment** | Need rule-based triggers | Want custom routing logic |2526**Rule of thumb**: Use Snippets for modifications, Workers for applications.2728## Execution Model291. Request arrives at Cloudflare edge302. Ruleset Engine evaluates snippet rules (filter expressions)313. If rule matches, snippet executes within 5ms limit324. Modified request/response continues through pipeline335. Response returned to client3435Snippets execute synchronously in the request path - performance is critical.3637## Reading Order381. **[configuration.md](configuration.md)** - Start here: setup, deployment methods (Dashboard/API/Terraform)392. **[api.md](api.md)** - Core APIs: Request, Response, headers, `request.cf` properties403. **[patterns.md](patterns.md)** - Real-world examples: geo-routing, A/B tests, security headers414. **[gotchas.md](gotchas.md)** - Troubleshooting: common errors, performance tips, API limitations4243## In This Reference4445- **[configuration.md](configuration.md)** - Setup, deployment, configuration46- **[api.md](api.md)** - API endpoints, methods, interfaces47- **[patterns.md](patterns.md)** - Common patterns, use cases, examples48- **[gotchas.md](gotchas.md)** - Troubleshooting, best practices, limitations4950## Quick Start51```javascript52// Snippet: Add security headers53export default {54async fetch(request) {55const response = await fetch(request);56const newResponse = new Response(response.body, response);57newResponse.headers.set("X-Frame-Options", "DENY");58newResponse.headers.set("X-Content-Type-Options", "nosniff");59return newResponse;60}61}62```6364Deploy via Dashboard (Rules → Snippets) or API/Terraform. See configuration.md for details.6566## See Also6768- [Cloudflare Docs](https://developers.cloudflare.com/rules/snippets/)69