跳转到内容
搜索文档

跟随源站重定向

修改 fetch 请求以跟随源站的重定向,确保客户端接收到最终响应。

最后更新 查看 MarkdownAgent 设置
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;
	},
};

此模板已准备就绪可供使用,并且应适用于大多数跟随重定向的场景。

它确保 Snippet 透明地跟随源服务器发出的重定向。Fetch APIredirect: "follow" 选项确保自动处理 3xx 重定向,返回最终响应。如果源站响应不是重定向,则返回原始内容。

这篇文档对您有帮助吗?