From 05affac34f3b6ef2dfe8ca842ed7e62bdc452bcd Mon Sep 17 00:00:00 2001 From: Taran Vohra Date: Fri, 28 Feb 2025 20:03:05 +0530 Subject: [PATCH] Add support for proxy sites using `x-gitbook-url` in v2 (#2901) --- packages/gitbook-v2/src/middleware.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/gitbook-v2/src/middleware.ts b/packages/gitbook-v2/src/middleware.ts index 81f63f329..2fa9d4677 100644 --- a/packages/gitbook-v2/src/middleware.ts +++ b/packages/gitbook-v2/src/middleware.ts @@ -107,11 +107,20 @@ function serveErrorResponse(error: Error) { } /** - * The URL of the GitBook content can be passed in 2 different ways: + * The URL of the GitBook content can be passed in 3 different ways: + * - The request URL is in the `X-GitBook-URL` header. * - Hostname is in the `X-GitBook-Host` header and the pathname is the path in the request URL. * - The request URL is matching `/url/:url` */ function extractURL(request: NextRequest): URLWithMode | null { + const xGitbookUrl = request.headers.get('x-gitbook-url'); + if (xGitbookUrl) { + return { + url: new URL(xGitbookUrl), + mode: 'url-host', + }; + } + const xGitbookHost = request.headers.get('x-gitbook-host'); if (xGitbookHost) { return {