Cloudflare Docs
Rules
Rules
Edit this page
Report an issue with this page
Log into the Cloudflare dashboard
Set theme to dark (⇧+D)

Cloudflare Snippets

Alpha

Cloudflare Snippets (alpha) provide a flexible way to customize the behavior of your website or application using short pieces of JavaScript code. Use snippets to customize HTTP response headers, implement JWT validation, define complex redirect functionality, and more.

​​ Snippet elements

To create and deploy a snippet you must define the following elements:

  • Snippet: Contains a name and the JavaScript code that will be executed as part of the request handling process.
  • Snippet rule: Contains a filter expression that will define for which requests the snippet will run.

​​ How it works

For each incoming request, Cloudflare evaluates the expression of each snippet rule defined in the zone checking for a match based on the request properties. Snippets are defined for each zone.

A snippet can run on every request or only on certain requests, based on various criteria such as bot score, country of origin, or a cookie.

Multiple snippets may run on the same request if their rule expressions match. This means that you could have a snippet adding an HTTP header and another snippet rewriting the URL, and they would both run if their corresponding expression matches the incoming request. Each snippet receives the modified request from the previous snippet and applies new modifications to it.

For each snippet rule whose expression matches the incoming request, the corresponding snippet code will be scheduled for execution. Cloudflare will apply the following logic for each snippet rule:

If <rule_expression> evaluates to true, then schedule <snippet_code> for execution

After evaluating all snippet rules, Cloudflare will execute the code of all scheduled snippets, in the same order their rules matched.

For more information, refer to our blog post.

​​ Availability

All customers on Enterprise plans have access to Cloudflare Snippets. Other paid plans will gradually get access throughout 2024. Once you have access, the Cloudflare dashboard will show a new Snippets tab under Rules at the zone level.

FreeProBusinessEnterprise

Availability

NoYesYesYes

Number of snippets

0102550

​​ Limits

Cloudflare Snippets are lightweight compared to Cloudflare Workers. The following limits apply:

DescriptionAll plans
Maximum execution time5 ms
Maximum memory2 MB
Maximum total package size32 KB
Subrequests
(refer to the note below)
1

​​ Execution order

The execution order of Rules features is the following:

The different types of rules listed above will take precedence over Page Rules (deprecated). This means that Page Rules will be overridden if there is a match for both Page Rules and the Rules products listed above.