From a0ce96820054d49a649156a3b74c67f789227c82 Mon Sep 17 00:00:00 2001 From: Nikita Cano Date: Tue, 3 Dec 2024 11:28:27 +0000 Subject: [PATCH 1/3] [Rules] Snippets example to follow redirects from origin --- .../snippets/examples/follow-redirects.mdx | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/content/docs/rules/snippets/examples/follow-redirects.mdx diff --git a/src/content/docs/rules/snippets/examples/follow-redirects.mdx b/src/content/docs/rules/snippets/examples/follow-redirects.mdx new file mode 100644 index 000000000000000..fc64d2c9c7be936 --- /dev/null +++ b/src/content/docs/rules/snippets/examples/follow-redirects.mdx @@ -0,0 +1,40 @@ +--- +type: example +summary: Modify the fetch request to follow redirects from the origin, ensuring the client receives the final response. +goal: + - Routing +operation: + - Redirect +products: + - Snippets +pcx_content_type: example +title: Follow redirects from the origin +description: Modify the fetch request to follow redirects from the origin, ensuring the client receives the final response. +--- + +```js +export default { + async fetch(request) { + // Define fetch options to follow redirects + const fetchOptions = { + redirect: "follow", // Ensure fetch follows redirects automatically. Remember that each subrequest in a redirect chain counts against 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; + }, +}; +``` + +This template is ready for use and should fit most redirect-following scenarios. + +It ensures the Snippet transparently follows redirects issued by the origin server. +The `fetch()` API's `redirect: "follow"` option ensures automatic handling of `3xx` redirects, returning the final response. If the origin response is not a redirect, the original content is returned. + +**Important**: Snippets have a [maximum number of subrequests per invocation](/rules/snippets/#availability) which depends on your plan. If the origin server issues multiple redirects, each redirect subrequest will count towards this limit. Ensure your origin configuration minimizes unnecessary redirects. \ No newline at end of file From 3dff5dd12b27ac1054688bea231dce790b639b85 Mon Sep 17 00:00:00 2001 From: Pedro Sousa <680496+pedrosousa@users.noreply.github.com> Date: Tue, 3 Dec 2024 12:47:13 +0000 Subject: [PATCH 2/3] PCX review --- .../snippets/examples/follow-redirects.mdx | 35 ++++++++++--------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/src/content/docs/rules/snippets/examples/follow-redirects.mdx b/src/content/docs/rules/snippets/examples/follow-redirects.mdx index fc64d2c9c7be936..c563c78930aa175 100644 --- a/src/content/docs/rules/snippets/examples/follow-redirects.mdx +++ b/src/content/docs/rules/snippets/examples/follow-redirects.mdx @@ -12,29 +12,30 @@ title: Follow redirects from the origin description: Modify the fetch request to follow redirects from the origin, ensuring the client receives the final response. --- -```js +```js wrap export default { - async fetch(request) { - // Define fetch options to follow redirects - const fetchOptions = { - redirect: "follow", // Ensure fetch follows redirects automatically. Remember that each subrequest in a redirect chain counts against subrequest limit. - }; + 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); + // 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}`); + // 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; - }, + // Return the final response to the client + return response; + }, }; ``` -This template is ready for use and should fit most redirect-following scenarios. +This template is ready for use and should fit most redirect-following scenarios. -It ensures the Snippet transparently follows redirects issued by the origin server. -The `fetch()` API's `redirect: "follow"` option ensures automatic handling of `3xx` redirects, returning the final response. If the origin response is not a redirect, the original content is returned. +It ensures the Snippet transparently follows redirects issued by the origin server. The Fetch API's `redirect: "follow"` option ensures automatic handling of `3xx` redirects, returning the final response. If the origin response is not a redirect, the original content is returned. -**Important**: Snippets have a [maximum number of subrequests per invocation](/rules/snippets/#availability) which depends on your plan. If the origin server issues multiple redirects, each redirect subrequest will count towards this limit. Ensure your origin configuration minimizes unnecessary redirects. \ No newline at end of file +:::note +Snippets have a [maximum number of subrequests per invocation](/rules/snippets/#availability) which depends on your plan. If the origin server issues multiple redirects, each redirect subrequest will count towards this limit. Ensure your origin configuration minimizes unnecessary redirects. +::: From 459ea4fd069c5f62d1a05433c94a39211de569ba Mon Sep 17 00:00:00 2001 From: Pedro Sousa <680496+pedrosousa@users.noreply.github.com> Date: Tue, 3 Dec 2024 15:05:15 +0000 Subject: [PATCH 3/3] Add links (Fetch API, 1202 error) --- src/content/docs/rules/snippets/examples/follow-redirects.mdx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/content/docs/rules/snippets/examples/follow-redirects.mdx b/src/content/docs/rules/snippets/examples/follow-redirects.mdx index c563c78930aa175..7130d5161e89a8f 100644 --- a/src/content/docs/rules/snippets/examples/follow-redirects.mdx +++ b/src/content/docs/rules/snippets/examples/follow-redirects.mdx @@ -34,8 +34,8 @@ export default { This template is ready for use and should fit most redirect-following scenarios. -It ensures the Snippet transparently follows redirects issued by the origin server. The Fetch API's `redirect: "follow"` option ensures automatic handling of `3xx` redirects, returning the final response. If the origin response is not a redirect, the original content is returned. +It ensures the Snippet transparently follows redirects issued by the origin server. The `redirect: "follow"` option of the [Fetch API](/workers/runtime-apis/fetch/) ensures automatic handling of `3xx` redirects, returning the final response. If the origin response is not a redirect, the original content is returned. :::note -Snippets have a [maximum number of subrequests per invocation](/rules/snippets/#availability) which depends on your plan. If the origin server issues multiple redirects, each redirect subrequest will count towards this limit. Ensure your origin configuration minimizes unnecessary redirects. +Snippets have a [maximum number of subrequests per invocation](/rules/snippets/#availability) which depends on your plan. If the origin server issues multiple redirects, each redirect subrequest will count towards this limit. When the number of subrequests exceeds the limit for your Cloudflare plan, you will receive a [1202 error](/rules/snippets/errors/#error-1202-snippets-exceeded-subrequests-limit). Ensure your origin configuration minimizes unnecessary redirects. :::