Skip to content

Commit 4234289

Browse files
authored
Fix incoming URL for requests that were proxied (#3127)
1 parent eedefdd commit 4234289

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

.changeset/fresh-peaches-smoke.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"gitbook-v2": patch
3+
---
4+
5+
Fix incoming URL for requests that were proxied

packages/gitbook-v2/src/middleware.ts

+10-7
Original file line numberDiff line numberDiff line change
@@ -148,18 +148,21 @@ async function serveSiteRoutes(requestURL: URL, request: NextRequest) {
148148
// correctly generated when the site is proxied. e.g. https://proxy.gitbook.com/site/siteId/...
149149
const siteCanonicalURL = new URL(siteURLData.canonicalUrl);
150150

151+
let incomingURL = requestURL;
152+
// For cases where the site is proxied, we use the canonical URL
153+
// as the incoming URL along with all the search params from the request.
154+
if (mode !== 'url') {
155+
incomingURL = siteCanonicalURL;
156+
incomingURL.search = requestURL.search;
157+
}
151158
//
152159
// Make sure the URL is clean of any va token after a successful lookup
153160
// The token is stored in a cookie that is set on the redirect response
154161
//
155-
const incomingURL = mode === 'url' ? requestURL : siteCanonicalURL;
156-
const requestURLWithoutToken = normalizeVisitorAuthURL(incomingURL);
157-
if (
158-
requestURLWithoutToken !== incomingURL &&
159-
requestURLWithoutToken.toString() !== incomingURL.toString()
160-
) {
162+
const incomingURLWithoutToken = normalizeVisitorAuthURL(incomingURL);
163+
if (incomingURLWithoutToken.toString() !== incomingURL.toString()) {
161164
return writeResponseCookies(
162-
NextResponse.redirect(requestURLWithoutToken.toString()),
165+
NextResponse.redirect(incomingURLWithoutToken.toString()),
163166
cookies
164167
);
165168
}

0 commit comments

Comments
 (0)