Need guidance on choosing between Snippets and Workers?
Explore our detailed guide for best practices, real-world use cases, and example implementations.
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.
To create and deploy a snippet, you need to define the following elements:
For more information, refer to How it works and Create a snippet in the dashboard.
Cloudflare provides you with rules templates for common use cases. In the dashboard, go to your zone > Rules > Overview, select Templates, and then select one of the available templates. You can also refer to the Examples gallery in the developer docs.
Free | Pro | Business | Enterprise | |
---|---|---|---|---|
Availability | No | Yes | Yes | Yes |
Number of snippets | 0 | 25 | 50 | 300 |
Number of subrequests | 0 | 2 | 3 | 5 |
Each subrequest in a redirect chain counts against the subrequest limit. This means that if a subrequest was redirected it would count as two subrequests. To avoid issues, ensure that you make a subrequest to the end location of the redirect chain.
Cloudflare Snippets are designed for fast, lightweight edge logic. The following limits apply:
Description | All plans |
---|---|
Maximum execution time | 5 ms |
Maximum memory | 2 MB |
Maximum total package size | 32 KB |
Need guidance on choosing between Snippets and Workers?
Explore our detailed guide for best practices, real-world use cases, and example implementations.
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.