Skip to content

非公式 / UnofficialCloudflare, Inc. 非提携。アカウント情報は入力しないでください。Not affiliated with Cloudflare, Inc. Do not enter Cloudflare credentials here.Official docsこのサイトについてプライバシー

オリジンからのリダイレクトをたどる

fetch リクエストを変更してオリジンからのリダイレクトをたどり、クライアントが最終レスポンスを受け取れるようにします。

最終更新 Markdown で表示Agent セットアップ
export default {
	async fetch(request) {
		// Define fetch options to follow redirects
		const fetchOptions = {
			redirect: "follow", // Ensure fetch follows redirects automatically. Each subrequest in a redirect chain counts against the subrequest limit.
		};

		// Make the fetch request to the origin
		const response = await fetch(request, fetchOptions);

		// Log the final URL after redirects (optional, for debugging)
		console.log(`Final URL after redirects: ${response.url}`);

		// Return the final response to the client
		return response;
	},
};

このテンプレートはそのまま使え、ほとんどのリダイレクト追跡の用途に向いています。

Snippets は、オリジンサーバーが返すリダイレクトを透過的にたどります。Fetch API の redirect: "follow" オプションにより、3xx リダイレクトは自動処理され、最終レスポンスが返ります。オリジンのレスポンスがリダイレクトでなければ、元の内容が返ります。

役に立ちましたか?