mirror of
https://github.com/vercel/next.js.git
synced 2026-09-20 02:25:18 +08:00
aaf28ab51c
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.
17 lines
580 B
TypeScript
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 });
|
|
}
|