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
.
import { Context } from "netlify:edge";
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, using this entry in the netlify.toml file:
[[edge_functions]]
function = "transform"
path = "/*"
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! And 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 an edge-functions directory in your project, and add [[edge_functions]]
entries to your netlify.toml file.
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.
Deploy to Netlify