From 85e05b303759a080ea91534442dfc47f4392d5ea Mon Sep 17 00:00:00 2001 From: Stefan Probst Date: Fri, 3 May 2024 10:31:33 +0200 Subject: [PATCH] fix: use x-forwarded-host header in preview urls --- app/api/keystatic/[...params]/route.ts | 24 +----------------------- app/api/preview/start/route.ts | 7 +++++-- lib/rewrite-url.ts | 23 +++++++++++++++++++++++ 3 files changed, 29 insertions(+), 25 deletions(-) create mode 100644 lib/rewrite-url.ts diff --git a/app/api/keystatic/[...params]/route.ts b/app/api/keystatic/[...params]/route.ts index b6cc67d4a..ee352340f 100644 --- a/app/api/keystatic/[...params]/route.ts +++ b/app/api/keystatic/[...params]/route.ts @@ -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)); } diff --git a/app/api/preview/start/route.ts b/app/api/preview/start/route.ts index 1a185bdc0..7e0af0a65 100644 --- a/app/api/preview/start/route.ts +++ b/app/api/preview/start/route.ts @@ -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; @@ -18,5 +21,5 @@ export function GET(request: Request): Response { toUrl.protocol = url.protocol; toUrl.host = url.host; - redirect(toUrl.toString()); + redirect(String(toUrl)); } diff --git a/lib/rewrite-url.ts b/lib/rewrite-url.ts new file mode 100644 index 000000000..ceab0e980 --- /dev/null +++ b/lib/rewrite-url.ts @@ -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; +}