Skip to content

非公式本サイトは非公式の日本語ドキュメントであり、Cloudflare 公式サイトではありません。最新情報はdevelopers.cloudflare.comをご確認ください。

遅延アクション

最終更新 Markdown で表示Agent セットアップ

Bot Management と Workers のサブスクリプションがあるお客様は、次のテンプレートで、ボットの可能性が高いリクエストに遅延を加えられます。

テンプレートは最小遅延と最大遅延を設定し、ボットスコアが 30 未満で、URI パスが /exampleURI で始まるリクエストを遅延します。

// Configurable Variables
const PATH_START = "/exampleURI";
const DELAY_FROM = 5; // in seconds
const DELAY_TO = 10; // in seconds

export default {
	async fetch(request, env, ctx) {
		const url = new URL(request.url);
		const botScore = request.cf.botManagement.score;

		if (url.pathname.startsWith(PATH_START) && botScore < 30) {
			// Random delay between DELAY_FROM and DELAY_TO seconds
			const delay =
				Math.floor(Math.random() * (DELAY_TO - DELAY_FROM + 1)) + DELAY_FROM;
			await new Promise((resolve) => setTimeout(resolve, delay * 1000));

			// Fetch the original request
			return fetch(request);
		}

		// Fetch the original request without delay
		return fetch(request);
	},
};
Workers templatets
// Configurable Variables
const PATH_START = '/exampleURI';
const DELAY_FROM = 5; // in seconds
const DELAY_TO = 10; // in seconds

export default {
  async fetch(request, env, ctx): Promise<Response> {
    const url = new URL(request.url);
    const botScore = request.cf.botManagement.score

    if (url.pathname.startsWith(PATH_START) && botScore < 30) {
      // Random delay between DELAY_FROM and DELAY_TO seconds
      const delay = Math.floor(Math.random() * (DELAY_TO - DELAY_FROM + 1)) + DELAY_FROM;
      await new Promise(resolve => setTimeout(resolve, delay * 1000));

      // Fetch the original request
      return fetch(request);
    }

    // Fetch the original request without delay
    return fetch(request);
  },
} satisfies ExportedHandler<Env>;

役に立ちましたか?