Skip to content

Commit

Permalink
fix: use x-forwarded-host header to rewrite hostname
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanprobst committed Mar 19, 2024
1 parent e0af91e commit b65d295
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion app/api/keystatic/[...params]/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
import { isNonEmptyString } from "@acdh-oeaw/lib";
import { makeRouteHandler } from "@keystatic/next/route-handler";

import config from "@/keystatic.config";

export const { GET, POST } = makeRouteHandler({ config });
const { GET: _GET, POST: _POST } = makeRouteHandler({ config });

/**
* @see https://github.com/Thinkmill/keystatic/issues/978#issuecomment-2005730530
* @see https://github.com/Thinkmill/keystatic/issues/1022
*/
function rewriteUrl(request: Request) {
const forwardedHost = request.headers.get("x-forwarded-host");
const forwardedProto = request.headers.get("x-forwarded-proto");

if (isNonEmptyString(forwardedHost) && isNonEmptyString(forwardedProto)) {
const url = new URL(request.url);

url.hostname = forwardedHost;
url.protocol = forwardedProto;

return new Request(url, request);
}

return request;
}

export function GET(request: Request) {
return _GET(rewriteUrl(request));
}

export function POST(request: Request) {
return _POST(rewriteUrl(request));
}

0 comments on commit b65d295

Please sign in to comment.