When to use Snippets vs Workers·续

Set CORS headers

Adjusts Cross-Origin Resource Sharing (CORS) ↗︎ headers and handles preflight requests.

// Define CORS headers
const corsHeaders = {
    "Access-Control-Allow-Origin": "*", // Replace * with your allowed origin(s)
    "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS", // Adjust allowed methods as needed
    "Access-Control-Allow-Headers": "Content-Type, Authorization", // Adjust allowed headers as needed
    "Access-Control-Max-Age": "86400", // Adjust max age (in seconds) as needed
};

export default {
    async fetch(request) {
        // Make a copy of the request to modify its headers
        const modifiedRequest = new Request(request);

// Handle preflight requests (OPTIONS)
        if (request.method === "OPTIONS") {
            return new Response(null, {
                headers: {
                    ...corsHeaders,
                },
                status: 200, // Respond with OK status for preflight requests
            });
        }

// Pass the modified request through to the origin
        const response = await fetch(modifiedRequest);

// Make a copy of the response to modify its headers
        const modifiedResponse = new Response(response.body, response);

// Set CORS headers on the response
        Object.keys(corsHeaders).forEach((header) => {
            modifiedResponse.headers.set(header, corsHeaders[header]);
        });

return modifiedResponse;
    },
};

Replaces outdated links without having to make changes on your origin.

export default {
    async fetch(request) {
        // Define the old hostname here.
        const OLD_URL = "oldsite.com";
        // Then add your new hostname that should replace the old one.
        const NEW_URL = "newsite.com";

class AttributeRewriter {
            constructor(attributeName) {
                this.attributeName = attributeName;
            }
            element(element) {
                const attribute = element.getAttribute(this.attributeName);
                if (attribute) {
                    element.setAttribute(
                        this.attributeName,
                        attribute.replace(OLD_URL, NEW_URL),
                    );
                }
            }
        }

const rewriter = new HTMLRewriter()
            .on("a", new AttributeRewriter("href"))
            .on("img", new AttributeRewriter("src"));

const res = await fetch(request);
        const contentType = res.headers.get("Content-Type");

// If the response is HTML, it can be transformed with
        // HTMLRewriter -- otherwise, it should pass through
        if (contentType.startsWith("text/html")) {
            return rewriter.transform(res);
        } else {
            return res;
        }
    },
};

Slow down requests

Defines a delay to be used when incoming requests match your rule. Useful for suspicious requests.

export default {
    async fetch(request) {
        // Define delay
        const delay_in_seconds = 5;
        // Introduce a delay
        await new Promise((resolve) =>
            setTimeout(resolve, delay_in_seconds * 1000),
        ); // Set delay in milliseconds

// Pass the request to the origin
        const response = await fetch(request);
        return response;
    },
};

Using Snippets and Workers together

While Snippets and Workers have distinct capabilities, they can work together to handle complex traffic workflows.

To avoid conflicts, Snippets and Workers should operate on separate request paths rather than running on the same URL. Have them fetch their respective URLs as a subrequest within their logic, ensuring smooth execution and caching behavior.

Example 1: Passing data between Snippets and Workers

Snippets can modify incoming requests before they reach a Worker, and Workers can read these modifications, perform additional transformations, and pass them downstream.

Snippet: Add a custom header

export default {
    async fetch(request) {
        // Get the current timestamp
        const timestamp = Date.now();
        const hexTimestamp = timestamp.toString(16);

// Clone request and add a custom header
        const modifiedRequest = new Request(request, {
            headers: new Headers(request.headers),
        });
        modifiedRequest.headers.set("X-Hex-Timestamp", hexTimestamp);

console.log(`X-Hex-Timestamp: ${hexTimestamp}`);

// Pass modified request to origin
        return fetch(modifiedRequest);
    },
};

Worker: Read a header and add it to the response

export default {
    async fetch(request) {
        const response = await fetch("https://{snippets_url}", request); // Ensure {snippets_url} points to the endpoint modified by Snippets
        const newResponse = new Response(response.body, response);

let hexTimestamp = request.headers.get("X-Hex-Timestamp") || "null";
        console.log(hexTimestamp);

newResponse.headers.set("X-Hex-Timestamp", hexTimestamp);
        return newResponse;
    },
};

Result: The Snippet sets X-Hex-Timestamp, which the Worker reads and forwards to the origin.

Example 2: Caching Worker responses using Snippets

A Worker performs compute-heavy processing (for example, image transformation), while a Snippet serves cached results to avoid unnecessary Worker execution. This can be helpful in situations when running Workers before cache is not desirable.

Worker: Transform and cache responses

export default {
    async fetch(request) {
        const url = new URL(request.url);
        url.hostname = "origin.example.com"; // Ensure this hostname points to the origin where the resource is hosted

const newRequest = new Request(url, request);
        const customKey = `https://${url.hostname}${url.pathname}`; // This custom cache key should be the same in both Worker and Snippet configuration for cache to work

// Fetch and modify response
        const response = await fetch(newRequest);
        const newResponse = new Response(response.body, response);

// Cache the transformed response
        const cache = caches.default;
        const cachedResponse = newResponse.clone();
        cachedResponse.headers.set("X-Cached-In-Workers", "true");
        await cache.put(customKey, cachedResponse);

newResponse.headers.set("X-Retrieved-From-Workers", "true");
        return newResponse;
    },
};

Snippet: Serve cached responses or forward to Worker

export default {
    async fetch(request) {
        const url = new URL(request.url);
        url.hostname = "origin.example.com"; // Ensure this hostname points to the origin where the resource is hosted
        const cacheKey = `https://${url.hostname}${url.pathname}`; // This custom cache key should be the same in both Worker and Snippet configuration for cache to work

// Access cache
        const cache = caches.default;
        let response = await cache.match(cacheKey);

if (!response) {
            console.log(`Cache miss for: ${cacheKey}. Fetching from Worker...`);
            url.hostname = "worker.example.com"; // Ensure this hostname points to the Workers route
            response = await fetch(new Request(url, request));

// Cache the response for future use
            response = new Response(response.body, response);
            response.headers.set("Cache-Control", `s-maxage=3600`);
            response.headers.set("x-snippets-cache", "stored");
        } else {
            console.log(`Cache hit for: ${cacheKey}`);
            response = new Response(response.body, response);
            response.headers.set("x-snippets-cache", "hit");
        }

return response;
    },
};

Result: The transformed response (X-Cached-In-Workers: true) is served from cache, avoiding redundant Worker execution (X-Retrieved-From-Workers is not present). When cache expires, the Snippet fetches a fresh version.


Migration between Snippets and Workers

Snippets and Workers share the same Workers runtime, meaning JavaScript code that does not rely on bindings, persistent storage, or advanced execution features can be migrated seamlessly between them.

When to migrate workloads to Snippets

You should consider migrating a Worker to Snippets if it:

  • Only modifies headers, redirects, caching rules, or origin routing.
  • Does not require bindings, persistent storage, or external integrations.
  • Is a lightweight JavaScript function with simple logic.
  • Needs to run an unlimited number of times for free on a Pro, Business, or Enterprise plan.

Migrating to Snippets allows you to:

  • Leverage advanced request matching via the Ruleset Engine.
  • Eliminate usage-based billing — Snippets are included at no cost on all paid plans.
  • Simplify management by integrating traffic modifications directly into Cloudflare Rules.

When to migrate workloads to Workers

You should migrate from Snippets to Workers if your logic:

If your Snippet reaches the limits of execution time, memory, or functionality, transitioning to Workers ensures your logic can scale without restrictions.


Conclusion

Cloudflare Snippets provide a production-ready solution for fast, declarative edge traffic logic, bridging the gap between Cloudflare Rules and Developer Platform.

Snippets and Workers solve different problems:

  • Use Snippets for fast, lightweight traffic modifications at the edge, including header rewrites, caching, redirects, origin routing, custom responses, A/B testing and authentication.
  • Workers are built for advanced compute, persistent state, and full-stack applications.

Was this helpful?

YesNo

On this page

When to use Snippets vs Workers·续 - 图1Docs

{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/rules/snippets/when-to-use/#page","headline":"When to use Snippets vs Workers","description":"This guide helps you determine when to use Snippets or Workers on Cloudflare's global network.","url":"https://developers.cloudflare.com/rules/snippets/when-to-use/","inLanguage":"en","image":"https://developers.cloudflare.com/rules/snippets/when-to-use/og.png?v=c051752b2216150f","dateModified":"2026-07-03","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"},"keywords":["Request modification","Response modification","Middleware"]}


description: Modify request URLs, headers, and response headers with Transform Rules. title: Transform Rules image: https://developers.cloudflare.com/rules/transform/og.png?v=660736d97caa60a2


Skip to content

Documentation Index
Fetch the complete documentation index at: https://developers.cloudflare.com/rules/llms.txt
Use this file to discover all available pages before exploring further.