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 API 的 redirect: "follow" 选项确保自动处理 3xx 重定向,返回最终响应。如果源站响应不是重定向,则返回原始内容。