You can use Edge Functions to transform the content of an HTTP response. In this example, we transform the response of a
request to /hello
with JavaScript's toUpperCase()
function, using the query parameter
?method=transform
.
Edge Functions are files held in the netlify/edge-functions
directory.
import { Context } from "@netlify/edge-functions";
export default async (request: Request, context: Context) => {
const url = new URL(request.url);
// Look for the query parameter, and return if we don't find it
if (url.searchParams.get("method") !== "transform") {
return;
}
console.log(`Transforming the response from this ${url}`);
const response = await context.next();
const text = await response.text();
return new Response(text.toUpperCase(), response);
};
You can deploy this and all the other examples in this repo as a site of your own to explore and experiment with, by clicking this button.