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
29 lines (24 loc) · 852 Bytes
/
proxy.ts
File metadata and controls
29 lines (24 loc) · 852 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
27
28
29
import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";
import { commerce } from "./lib/commerce";
export async function proxy(request: NextRequest) {
const { store, publicUrl } = await commerce.meGet();
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(
`/${store.subdomain}${request.nextUrl.pathname}${request.nextUrl.search}`,
destinationUrl,
);
return NextResponse.rewrite(url, {
request: {
headers: requestHeaders,
},
});
}
export const config = {
matcher: ["/checkout/:path*"],
};