Files
Steven aaf28ab51c chore(example): add example for image optimization with secure compute (#72641)
This PR adds a new example that shows a more complex use case where you
don't want browser to know about the upstream image server so instead
you can proxy through a serverless function.

Some use cases might be checking for an IP address, user agent, etc.
2024-11-13 11:51:01 -05:00

17 lines
580 B
TypeScript

// TODO: set origin here
const SOURCE_IMAGE_ORIGIN = "https://image-optimization-one.vercel.app";
export async function GET(req: Request) {
if (!req.headers.get("user-agent")) {
// TODO: add any other checks here
return new Response("Forbidden", { status: 403 });
}
const path = new URL(req.url).pathname.slice("/image-api".length);
if (!path) {
return new Response("Bad Request", { status: 400 });
}
const res = await fetch(new URL(path, SOURCE_IMAGE_ORIGIN));
const { status, headers, body } = res;
return new Response(body, { status, headers });
}