From 42342893d6749603d5516d89c3724e5c6ec168ce Mon Sep 17 00:00:00 2001 From: Taran Vohra Date: Wed, 9 Apr 2025 19:10:14 +0530 Subject: [PATCH] Fix incoming URL for requests that were proxied (#3127) --- .changeset/fresh-peaches-smoke.md | 5 +++++ packages/gitbook-v2/src/middleware.ts | 17 ++++++++++------- 2 files changed, 15 insertions(+), 7 deletions(-) create mode 100644 .changeset/fresh-peaches-smoke.md diff --git a/.changeset/fresh-peaches-smoke.md b/.changeset/fresh-peaches-smoke.md new file mode 100644 index 000000000..3801a693e --- /dev/null +++ b/.changeset/fresh-peaches-smoke.md @@ -0,0 +1,5 @@ +--- +"gitbook-v2": patch +--- + +Fix incoming URL for requests that were proxied diff --git a/packages/gitbook-v2/src/middleware.ts b/packages/gitbook-v2/src/middleware.ts index a41d86807..dd983b536 100644 --- a/packages/gitbook-v2/src/middleware.ts +++ b/packages/gitbook-v2/src/middleware.ts @@ -148,18 +148,21 @@ async function serveSiteRoutes(requestURL: URL, request: NextRequest) { // correctly generated when the site is proxied. e.g. https://proxy.gitbook.com/site/siteId/... const siteCanonicalURL = new URL(siteURLData.canonicalUrl); + let incomingURL = requestURL; + // For cases where the site is proxied, we use the canonical URL + // as the incoming URL along with all the search params from the request. + if (mode !== 'url') { + incomingURL = siteCanonicalURL; + incomingURL.search = requestURL.search; + } // // Make sure the URL is clean of any va token after a successful lookup // The token is stored in a cookie that is set on the redirect response // - const incomingURL = mode === 'url' ? requestURL : siteCanonicalURL; - const requestURLWithoutToken = normalizeVisitorAuthURL(incomingURL); - if ( - requestURLWithoutToken !== incomingURL && - requestURLWithoutToken.toString() !== incomingURL.toString() - ) { + const incomingURLWithoutToken = normalizeVisitorAuthURL(incomingURL); + if (incomingURLWithoutToken.toString() !== incomingURL.toString()) { return writeResponseCookies( - NextResponse.redirect(requestURLWithoutToken.toString()), + NextResponse.redirect(incomingURLWithoutToken.toString()), cookies ); }