Skip to content

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

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

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 APIredirect: "follow" オプションにより、3xx リダイレクトは自動処理され、最終レスポンスが返ります。オリジンのレスポンスがリダイレクトでなければ、元の内容が返ります。

役に立ちましたか?