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

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. This means that Page Rules will be overridden if there is a match for both Page Rules and the Rules products listed above.

Generally speaking, for non-terminating actions the last change made by rules in the same phase will win (later rules can overwrite changes done by previous rules). However, for terminating actions (Block, Redirect, or one of the challenge actions), rule evaluation will stop and the action will be executed immediately.

For example, if multiple rules with the Redirect action match, Cloudflare will always use the URL redirect of the first rule that matches. Also, if you configure URL redirects using different Cloudflare products (Single Redirects and Bulk Redirects), the product executed first will apply, if there is a rule match (in this case, Single Redirects). Refer to the Phases list for the product execution order.