Implementing 302 redirect follow by Edge Routine helps you reduce interaction times, improve access speed, and enhance user experience.
When to use it
If your business experiences high load and you need to manage redirection responses from your original server, use the 302 redirect follow feature. This feature allows ESA to automatically follow 302 redirects to fetch the final resource.
Sample code
Expected effect: When a client sends a request and the origin server returns a 302 status code, the ESA point of presence (POP) automatically follows the redirect and delivers the redirected result to the client.
Language:
JavascriptExample:
/** * fetch is a customized routine for handling requests from the client. * When the status code returned from the origin is 302 (temporary redirect), * ESA POP follows the redirection and returns the result of the redirected request to the client. * Example URL of client request: https://example.com/redirect * Example URL of redirection target: https://example.com/target */ export default { async fetch(request) { // Sends a request to the Edge Routine and manually handles the redirect. // Uses the { redirect: "manual" } option to ensure that redirection is not automatically followed, // so that responses with a status code of 302 can be handled by the Edge Routine. const response = await fetch(request, { redirect: "manual" }); // Gets the status code of the response. const status = response.status; // If the response status code is 302 (temporary redirect), if (status === 302) { // gets the target address of the redirection from the response header. const redirectLocation = response.headers.get("Location"); // Sends a new request to the redirection target address. const res = await fetch(redirectLocation); // Returns the result of the redirected request to the client. return res; } // If the response status code is not 302, return the response of the Edge Routine. return response; }, };
Deployment effect
When the response status code is 302:
The POP obtains the
https://example.com/targetfrom theLocationheader in the response, then sends a new request to this URL and returns the response to the client. The client receives the response fromhttps://example.com/targetwithout knowing that a redirection occurred.Client requested URL: https://example.com/redirect //The original URL requested by the client is https://example.com/redirect. Redirecting from https://example.com/redirect to https://example.com/target //The origin sever returned a 302 status code, requiring redirection to https://example.com/target.When the response status code is not 302:
The client directly receives the response content from the Edge Routine.
Client requested URL: https://example.com/redirect //The original URL requested by the client is https://example.com/redirect.