@@ -63,7 +63,7 @@ async function serveSiteRoutes(requestURL: URL, request: NextRequest) {
63
63
return null ;
64
64
}
65
65
66
- const { url : siteURL , mode } = match ;
66
+ const { url : siteRequestURL , mode } = match ;
67
67
68
68
/**
69
69
* Serve image resizing requests (all requests containing `/~gitbook/image`).
@@ -73,9 +73,9 @@ async function serveSiteRoutes(requestURL: URL, request: NextRequest) {
73
73
* In GitBook v1: image resizing was done at the root of the hostname (docs.company.com/~gitbook/image)
74
74
* In GitBook v2: image resizing is done at the content level (docs.company.com/section/variant/~gitbook/image)
75
75
*/
76
- if ( siteURL . pathname . endsWith ( '/~gitbook/image' ) ) {
76
+ if ( siteRequestURL . pathname . endsWith ( '/~gitbook/image' ) ) {
77
77
return await serveResizedImage ( request , {
78
- host : siteURL . host ,
78
+ host : siteRequestURL . host ,
79
79
} ) ;
80
80
}
81
81
@@ -85,13 +85,13 @@ async function serveSiteRoutes(requestURL: URL, request: NextRequest) {
85
85
// @ts -ignore - request typing
86
86
const visitorToken = getVisitorToken ( {
87
87
cookies : request . cookies . getAll ( ) ,
88
- url : siteURL ,
88
+ url : siteRequestURL ,
89
89
} ) ;
90
90
91
91
const withAPIToken = async ( apiToken : string | null ) => {
92
92
const siteURLData = await throwIfDataError (
93
93
getPublishedContentByURL ( {
94
- url : siteURL . toString ( ) ,
94
+ url : siteRequestURL . toString ( ) ,
95
95
visitorAuthToken : visitorToken ?. token ?? null ,
96
96
// When the visitor auth token is pulled from the cookie, set redirectOnError when calling getPublishedContentByUrl to allow
97
97
// 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) {
136
136
137
137
cookies . push ( ...getResponseCookiesForVisitorAuth ( siteURLData . siteBasePath , visitorToken ) ) ;
138
138
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
+
139
143
//
140
144
// Make sure the URL is clean of any va token after a successful lookup
141
145
// The token is stored in a cookie that is set on the redirect response
142
146
//
143
- const incomingURL = mode === 'url' ? requestURL : siteURL ;
147
+ const incomingURL = mode === 'url' ? requestURL : siteCanonicalURL ;
144
148
const requestURLWithoutToken = normalizeVisitorAuthURL ( incomingURL ) ;
145
149
if (
146
150
requestURLWithoutToken !== incomingURL &&
@@ -152,10 +156,6 @@ async function serveSiteRoutes(requestURL: URL, request: NextRequest) {
152
156
) ;
153
157
}
154
158
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
-
159
159
//
160
160
// Render and serve the content
161
161
//
@@ -174,12 +174,12 @@ async function serveSiteRoutes(requestURL: URL, request: NextRequest) {
174
174
requestHeaders . set ( MiddlewareHeaders . SiteURLData , JSON . stringify ( siteURLData ) ) ;
175
175
176
176
// Preview of customization/theme
177
- const customization = siteURL . searchParams . get ( 'customization' ) ;
177
+ const customization = siteRequestURL . searchParams . get ( 'customization' ) ;
178
178
if ( customization && validateSerializedCustomization ( customization ) ) {
179
179
routeType = 'dynamic' ;
180
180
requestHeaders . set ( MiddlewareHeaders . Customization , customization ) ;
181
181
}
182
- const theme = siteURL . searchParams . get ( 'theme' ) ;
182
+ const theme = siteRequestURL . searchParams . get ( 'theme' ) ;
183
183
if ( theme === CustomizationThemeMode . Dark || theme === CustomizationThemeMode . Light ) {
184
184
routeType = 'dynamic' ;
185
185
requestHeaders . set ( MiddlewareHeaders . Theme , theme ) ;
@@ -260,10 +260,10 @@ async function serveSiteRoutes(requestURL: URL, request: NextRequest) {
260
260
} ;
261
261
262
262
// For https://preview/<siteURL> requests,
263
- if ( siteURL . hostname === 'preview' ) {
263
+ if ( siteRequestURL . hostname === 'preview' ) {
264
264
return serveWithQueryAPIToken (
265
265
// 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 ] } ` ,
267
267
request ,
268
268
withAPIToken
269
269
) ;
0 commit comments