Skip to content

Commit

Permalink
fix: use x-forwarded-host header in preview urls
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanprobst committed May 3, 2024
1 parent cb5a4c8 commit 85e05b3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 25 deletions.
24 changes: 1 addition & 23 deletions app/api/keystatic/[...params]/route.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,10 @@
import { isNonEmptyString } from "@acdh-oeaw/lib";
import { makeRouteHandler } from "@keystatic/next/route-handler";

import config from "@/keystatic.config";
import { rewriteUrl } from "@/lib/rewrite-url";

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");
const forwardedPort = request.headers.get("x-forwarded-port");

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

url.hostname = forwardedHost;
url.protocol = forwardedProto;
url.port = forwardedPort ?? "";

return new Request(url, request);
}

return request;
}

export function GET(request: Request) {
return _GET(rewriteUrl(request));
}
Expand Down
7 changes: 5 additions & 2 deletions app/api/preview/start/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { cookies, draftMode } from "next/headers";
import { redirect } from "next/navigation";

export function GET(request: Request): Response {
import { rewriteUrl } from "@/lib/rewrite-url";

export function GET(_request: Request): Response {
const request = rewriteUrl(_request);
const url = new URL(request.url);
const params = url.searchParams;

Expand All @@ -18,5 +21,5 @@ export function GET(request: Request): Response {
toUrl.protocol = url.protocol;
toUrl.host = url.host;

redirect(toUrl.toString());
redirect(String(toUrl));
}
23 changes: 23 additions & 0 deletions lib/rewrite-url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { isNonEmptyString } from "@acdh-oeaw/lib";

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

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

url.hostname = forwardedHost;
url.protocol = forwardedProto;
url.port = forwardedPort ?? "";

return new Request(url, request);
}

return request;
}

0 comments on commit 85e05b3

Please sign in to comment.