Skip to content

Snippets

Cloudflare Snippets (beta) provide a powerful and flexible way to customize the behavior of your website or application using short pieces of JavaScript code. With Snippets, you can modify HTTP response headers, implement JWT validation, perform complex redirects, and much more.

For code samples addressing common use cases, please refer to the Examples section.

Snippets elements

To create and deploy a Snippet, you need to define the following elements:

  • Code snippet: JavaScript code to be executed during the request-handling process.
  • Snippet rule: A filter expression that determines which requests the Snippet will be applied to. Each Snippet can only be associated with one Snippet Rule.

For more information, refer to the How it works and Create in the dashboard sections.

Templates

Cloudflare provides you with rules templates for common use cases. In the dashboard, go to your zone > Rules > Templates and select one of the available templates. You can also refer to the Examples gallery in the developer docs.

Availability

Free Pro Business Enterprise

Availability

No

Yes

Yes

Yes

Number of snippets

0 10 25 50
Number of subrequests 0 2 3 5

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

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.