When to use Snippets vs Workers
This guide helps you determine when to use Snippets or Workers on Cloudflare's global network. It provides best practices, comparisons, and real-world use cases to help you choose the right product for your workload.
Cloudflare Snippets provide a fast, declarative way to modify HTTP requests and responses at the edge — without requiring a full-stack compute platform. Snippets extend Cloudflare Rules by allowing you to write JavaScript-based logic that modifies requests before they reach an origin and responses after they return from upstream.
Snippets enable you to:
- Modify headers, validate JWTs, and implement complex rewrites or redirects.
- Retry failed requests to different origins and apply custom caching strategies.
- Execute multiple Snippets sequentially, with each Snippet modifying the request or response before handing it off to the next.
Snippets are included at no additional cost in all paid plans, making them the preferred solution for lightweight edge logic.
By contrast, Cloudflare Workers provide a full-stack compute platform designed for applications requiring state, compute, and integrations with Cloudflare’s Developer Platform. Workers operate on a usage-based pricing model and include a free tier.
Snippets are ideal for fast, cost-free request and response modifications at the edge. They extend Cloudflare Rules without requiring additional infrastructure or external solutions.
- Ultra-fast traffic modifications applied directly on Cloudflare's network.
- Extend Cloudflare Rules beyond built-in actions for greater control.
- Simplify CDN migrations by replacing VCL, EdgeWorkers, or on-premise logic.
- Modify headers, cache responses, and perform redirects.
- Integrate edge logic into development workflows using JavaScript.
- Persistent state management (for example, session storage or databases).
- Compute-intensive tasks (for example, image transformations or AI inference).
- Deep integrations with Developer Platform services like Durable Objects or D1.
- Use cases requiring advanced runtime features, such as:
- Ultra-fast, edge-optimized execution, powered by Ruleset Engine and Workers runtime.
- Included at no additional cost on all paid plans.
- Granular request matching using dozens of request attributes, such as URI, user-agent, and cookies.
- Sequential execution – multiple Snippets can run on the same request, applying modifications step by step.
- Native integration with Cloudflare Rules – Snippets inherit request modifications from other products running in earlier request phases.
- JavaScript and Web APIs support, including:
- Essential Workers runtime features, such as:
- Automated deployment and versioning via Terraform.
Feature | Snippets | Workers |
---|---|---|
Execute scripts based on request attributes (for example, headers, geolocation, and cookies) | ✅ | ❌ |
Execute code on a specific URL route | ✅ | ✅ |
Modify HTTP requests/responses or serve a different response | ✅ | ✅ |
Add, remove, or rewrite headers dynamically | ✅ | ✅ |
Cache assets at the edge | ✅ | ✅ |
Route traffic dynamically between origin servers | ✅ | ✅ |
Authenticate requests, pre-sign URLs, run A/B testing | ✅ | ✅ |
Define logic using JavaScript and Web APIs | ✅ | ✅ |
Perform compute-heavy tasks (for example, AI, image transformations) | ❌ | ✅ |
Store persistent data (for example, KV, Durable Objects, and D1) | ❌ | ✅ |
Build APIs and full-stack applications | ❌ | ✅ |
Use TypeScript, Python, Rust, or other programming languages | ❌ | ✅ |
Support non-HTTP protocols | ❌ | ✅ |
Analyze execution logs and track performance metrics | ❌ | ✅ |
Deploy via command-line interface (CLI) | ❌ | ✅ |
Roll out gradually, roll back to previous versions | ❌ | ✅ |
Optimize execution with Smart Placement | ❌ | ✅ |
Below are practical use cases demonstrating Snippets in action. You can find more templates to get started in the Examples section.
Modifies request and response headers dynamically.
export default { async fetch(request) { // Get the current timestamp const timestamp = Date.now();
// Convert the timestamp to hexadecimal format const hexTimestamp = timestamp.toString(16);
// Clone the request and add the custom header with HEX timestamp const modifiedRequest = new Request(request, { headers: new Headers(request.headers), }); modifiedRequest.headers.set("X-Hex-Timestamp", hexTimestamp);
// Pass the modified request to the origin const response = await fetch(modifiedRequest);
// Clone the response so that it's no longer immutable const newResponse = new Response(response.body, response);
// Add a custom header with a value to the response newResponse.headers.append( "x-snippets-hello", "Hello from Cloudflare Snippets", );
// Delete headers from the response newResponse.headers.delete("x-header-to-delete"); newResponse.headers.delete("x-header2-to-delete");
// Adjust the value for an existing header in the response newResponse.headers.set("x-header-to-change", "NewValue");
// Serve modified response to the visitor return newResponse; },};
Routes traffic to a maintenance page when your origin is undergoing a planned maintenance.
export default { async fetch(request) { return new Response( ` <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>We'll Be Right Back!</title> <style> body { font-family: Arial, sans-serif; text-align: center; padding: 20px; } </style> </head> <body> <h1>We'll Be Right Back!</h1> <p>Our site is undergoing maintenance. Check back soon!</p> </body> </html> `, { status: 503, headers: { "Content-Type": "text/html" } }, ); },};