@@ -63,7 +63,7 @@ async function serveSiteRoutes(requestURL: URL, request: NextRequest) {
6363 return null ;
6464 }
6565
66- const { url : siteURL , mode } = match ;
66+ const { url : siteRequestURL , mode } = match ;
6767
6868 /**
6969 * Serve image resizing requests (all requests containing `/~gitbook/image`).
@@ -73,9 +73,9 @@ async function serveSiteRoutes(requestURL: URL, request: NextRequest) {
7373 * In GitBook v1: image resizing was done at the root of the hostname (docs.company.com/~gitbook/image)
7474 * In GitBook v2: image resizing is done at the content level (docs.company.com/section/variant/~gitbook/image)
7575 */
76- if ( siteURL . pathname . endsWith ( '/~gitbook/image' ) ) {
76+ if ( siteRequestURL . pathname . endsWith ( '/~gitbook/image' ) ) {
7777 return await serveResizedImage ( request , {
78- host : siteURL . host ,
78+ host : siteRequestURL . host ,
7979 } ) ;
8080 }
8181
@@ -85,13 +85,13 @@ async function serveSiteRoutes(requestURL: URL, request: NextRequest) {
8585 // @ts -ignore - request typing
8686 const visitorToken = getVisitorToken ( {
8787 cookies : request . cookies . getAll ( ) ,
88- url : siteURL ,
88+ url : siteRequestURL ,
8989 } ) ;
9090
9191 const withAPIToken = async ( apiToken : string | null ) => {
9292 const siteURLData = await throwIfDataError (
9393 getPublishedContentByURL ( {
94- url : siteURL . toString ( ) ,
94+ url : siteRequestURL . toString ( ) ,
9595 visitorAuthToken : visitorToken ?. token ?? null ,
9696 // When the visitor auth token is pulled from the cookie, set redirectOnError when calling getPublishedContentByUrl to allow
9797 // redirecting when the token is invalid as we could be dealing with stale token stored in the cookie.
@@ -136,11 +136,15 @@ async function serveSiteRoutes(requestURL: URL, request: NextRequest) {
136136
137137 cookies . push ( ...getResponseCookiesForVisitorAuth ( siteURLData . siteBasePath , visitorToken ) ) ;
138138
139+ // We use the host/origin from the canonical URL to ensure the links are
140+ // correctly generated when the site is proxied. e.g. https://proxy.gitbook.com/site/siteId/...
141+ const siteCanonicalURL = new URL ( siteURLData . canonicalUrl ) ;
142+
139143 //
140144 // Make sure the URL is clean of any va token after a successful lookup
141145 // The token is stored in a cookie that is set on the redirect response
142146 //
143- const incomingURL = mode === 'url' ? requestURL : siteURL ;
147+ const incomingURL = mode === 'url' ? requestURL : siteCanonicalURL ;
144148 const requestURLWithoutToken = normalizeVisitorAuthURL ( incomingURL ) ;
145149 if (
146150 requestURLWithoutToken !== incomingURL &&
@@ -152,10 +156,6 @@ async function serveSiteRoutes(requestURL: URL, request: NextRequest) {
152156 ) ;
153157 }
154158
155- // We use the host/origin from the canonical URL to ensure the links are
156- // correctly generated when the site is proxied. e.g. https://proxy.gitbook.com/site/siteId/...
157- const siteCanonicalURL = new URL ( siteURLData . canonicalUrl ) ;
158-
159159 //
160160 // Render and serve the content
161161 //
@@ -174,12 +174,12 @@ async function serveSiteRoutes(requestURL: URL, request: NextRequest) {
174174 requestHeaders . set ( MiddlewareHeaders . SiteURLData , JSON . stringify ( siteURLData ) ) ;
175175
176176 // Preview of customization/theme
177- const customization = siteURL . searchParams . get ( 'customization' ) ;
177+ const customization = siteRequestURL . searchParams . get ( 'customization' ) ;
178178 if ( customization && validateSerializedCustomization ( customization ) ) {
179179 routeType = 'dynamic' ;
180180 requestHeaders . set ( MiddlewareHeaders . Customization , customization ) ;
181181 }
182- const theme = siteURL . searchParams . get ( 'theme' ) ;
182+ const theme = siteRequestURL . searchParams . get ( 'theme' ) ;
183183 if ( theme === CustomizationThemeMode . Dark || theme === CustomizationThemeMode . Light ) {
184184 routeType = 'dynamic' ;
185185 requestHeaders . set ( MiddlewareHeaders . Theme , theme ) ;
@@ -260,10 +260,10 @@ async function serveSiteRoutes(requestURL: URL, request: NextRequest) {
260260 } ;
261261
262262 // For https://preview/<siteURL> requests,
263- if ( siteURL . hostname === 'preview' ) {
263+ if ( siteRequestURL . hostname === 'preview' ) {
264264 return serveWithQueryAPIToken (
265265 // We scope the API token to the site ID.
266- `${ siteURL . hostname } /${ requestURL . pathname . slice ( 1 ) . split ( '/' ) [ 0 ] } ` ,
266+ `${ siteRequestURL . hostname } /${ requestURL . pathname . slice ( 1 ) . split ( '/' ) [ 0 ] } ` ,
267267 request ,
268268 withAPIToken
269269 ) ;
0 commit comments