|
|
3 hari lalu | |
|---|---|---|
| .. | ||
| README.md | 3 hari lalu | |
| api.md | 3 hari lalu | |
| configuration.md | 3 hari lalu | |
| gotchas.md | 3 hari lalu | |
| patterns.md | 3 hari lalu | |
Expert 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).
Snippets are JavaScript functions executed at the edge as part of Cloudflare's Ruleset Engine. Key characteristics:
| Factor | Choose Snippets If... | Choose Workers If... |
|---|---|---|
| Complexity | Simple request/response modifications | Complex business logic, routing, middleware |
| Execution time | <5ms sufficient | Need >5ms or variable time |
| Subrequests | 2-5 fetch calls sufficient | Need >5 subrequests or complex orchestration |
| Code size | <32KB sufficient | Need >32KB or npm dependencies |
| Cost | Want zero additional cost | Can afford $5/mo + usage |
| APIs | Need basic fetch, headers, URL | Need KV, D1, R2, Durable Objects, cron triggers |
| Deployment | Need rule-based triggers | Want custom routing logic |
Rule of thumb: Use Snippets for modifications, Workers for applications.
Snippets execute synchronously in the request path - performance is critical.
request.cf properties// Snippet: Add security headers
export default {
async fetch(request) {
const response = await fetch(request);
const newResponse = new Response(response.body, response);
newResponse.headers.set("X-Frame-Options", "DENY");
newResponse.headers.set("X-Content-Type-Options", "nosniff");
return newResponse;
}
}
Deploy via Dashboard (Rules → Snippets) or API/Terraform. See configuration.md for details.