Transform responses
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 a toUpperCase()
function, using the query parameter method=transform
.
This is a simple example, and for more complex transformations you may want to use HTMLRewriter.
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;
}
const response = await context.next();
const text = await response.text();
return new Response(text.toUpperCase(), response);
};
This transform Edge Function is set up to run using the method=transform
query parameter on any URL
See this in action
- View the original response from /hello without a transform
- View the response from /hello dynamically transformed with an Edge Function
- The Edge Function code: transform.ts
What are Edge Functions?
Using JavaScript and TypeScript, Netlify Edge Functions give you the power to modify network requests to localize content, serve relevant ads, authenticate visitors, A/B test content, and much more!
This all happens at the Edge — directly from the worldwide location closest to each user.
To use Edge Functions on Netlify, add JavaScript or TypeScript files to a
/netlify/edge-functions
directory in your project.
Deploy this site to Netlify
Try out Edge Functions on Netlify today! Click the button below to deploy this site with all of its demos to your Netlify account.