コンテンツにスキップ

スニペット

アルファ

Cloudflare スニペット (アルファ) は、短い JavaScript コードを使用して、ウェブサイトやアプリケーションの動作をカスタマイズするための強力で柔軟な方法を提供します。スニペットを使用すると、HTTP レスポンスヘッダーを変更したり、JWT バリデーションを実装したり、複雑な リダイレクト を実行したり、その他多くのことが可能です。

一般的なユースケースに関するコードサンプルについては、 セクションを参照してください。

スニペット要素

スニペットを作成してデプロイするには、以下の要素を定義する必要があります:

  • スニペット: リクエスト処理プロセス中に実行される JavaScript コード。
  • スニペットルール: スニペットが適用されるリクエストを決定する フィルター式 。各スニペットは、1 つのスニペットルールにのみ関連付けることができます。

詳細については、動作の仕組み および ダッシュボードでの作成 セクションを参照してください。

テンプレート

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.

利用可能性

Free Pro Business Enterprise

Availability

No

Yes

Yes

Yes

Number of snippets

0 10 25 50
Number of subrequests 0 2 3 5

制限

Cloudflare スニペットは、Cloudflare Workers に比べて軽量です。以下の制限が適用されます:

説明すべてのプラン
最大実行時間5 ms
最大メモリ2 MB
最大パッケージサイズ32 KB

実行順序

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.