forked from yournextstore/yournextstore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
26 lines (21 loc) · 862 Bytes
/
proxy.ts
File metadata and controls
26 lines (21 loc) · 862 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";
import { getSubdomainPublicUrl } from "./lib/commerce";
export async function proxy(request: NextRequest) {
const { subdomain, publicUrl } = await getSubdomainPublicUrl();
const destinationUrl = new URL(publicUrl);
// Clone the request headers and set the correct x-forwarded-host
const requestHeaders = new Headers(request.headers);
requestHeaders.set("x-forwarded-host", destinationUrl.host);
requestHeaders.set("origin", destinationUrl.toString());
// Rewrite to the destination with updated headers
const url = new URL(`/${subdomain}${request.nextUrl.pathname}${request.nextUrl.search}`, destinationUrl);
return NextResponse.rewrite(url, {
request: {
headers: requestHeaders,
},
});
}
export const config = {
matcher: ["/checkout/:path*"],
};