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 ); }