You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/docs/rules/snippets/examples/follow-redirects.mdx
+18-17Lines changed: 18 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,29 +12,30 @@ title: Follow redirects from the origin
12
12
description: Modify the fetch request to follow redirects from the origin, ensuring the client receives the final response.
13
13
---
14
14
15
-
```js
15
+
```js wrap
16
16
exportdefault {
17
-
asyncfetch(request) {
18
-
// Define fetch options to follow redirects
19
-
constfetchOptions= {
20
-
redirect:"follow", // Ensure fetch follows redirects automatically. Remember that each subrequest in a redirect chain counts against subrequest limit.
21
-
};
17
+
asyncfetch(request) {
18
+
// Define fetch options to follow redirects
19
+
constfetchOptions= {
20
+
redirect:"follow", // Ensure fetch follows redirects automatically. Each subrequest in a redirect chain counts against the subrequest limit.
21
+
};
22
22
23
-
// Make the fetch request to the origin
24
-
constresponse=awaitfetch(request, fetchOptions);
23
+
// Make the fetch request to the origin
24
+
constresponse=awaitfetch(request, fetchOptions);
25
25
26
-
// Log the final URL after redirects (optional, for debugging)
27
-
console.log(`Final URL after redirects: ${response.url}`);
26
+
// Log the final URL after redirects (optional, for debugging)
27
+
console.log(`Final URL after redirects: ${response.url}`);
28
28
29
-
// Return the final response to the client
30
-
return response;
31
-
},
29
+
// Return the final response to the client
30
+
return response;
31
+
},
32
32
};
33
33
```
34
34
35
-
This template is ready for use and should fit most redirect-following scenarios.
35
+
This template is ready for use and should fit most redirect-following scenarios.
36
36
37
-
It ensures the Snippet transparently follows redirects issued by the origin server.
38
-
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.
37
+
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.
39
38
40
-
**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.
39
+
:::note
40
+
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.
0 commit comments