@@ -8,7 +8,7 @@ import { removeLeadingSlash, removeTrailingSlash } from '@/lib/paths';
8
8
import { getResponseCookiesForVisitorAuth , getVisitorToken } from '@/lib/visitor-token' ;
9
9
import { serveResizedImage } from '@/routes/image' ;
10
10
import { getPublishedContentByURL } from '@v2/lib/data' ;
11
- import { GITBOOK_URL } from '@v2/lib/env' ;
11
+ import { GITBOOK_ASSETS_URL , GITBOOK_URL } from '@v2/lib/env' ;
12
12
import { MiddlewareHeaders } from '@v2/lib/middleware' ;
13
13
14
14
export const config = {
@@ -20,7 +20,7 @@ type URLWithMode = { url: URL; mode: 'url' | 'url-host' };
20
20
export async function middleware ( request : NextRequest ) {
21
21
try {
22
22
// Route all requests to a site
23
- const extracted = extractURL ( request ) ;
23
+ const extracted = getSiteURLFromRequest ( request ) ;
24
24
if ( extracted ) {
25
25
/**
26
26
* Serve image resizing requests (all requests containing `/~gitbook/image`).
@@ -114,7 +114,7 @@ async function serveSiteByURL(request: NextRequest, urlWithMode: URLWithMode) {
114
114
encodePathInSiteContent ( data . pathname ) ,
115
115
] . join ( '/' ) ;
116
116
117
- console . log ( `rewriting to ${ route } ` ) ;
117
+ console . log ( `rewriting ${ request . nextUrl . toString ( ) } to ${ route } ` ) ;
118
118
119
119
const rewrittenURL = new URL ( `/${ route } ` , request . nextUrl . toString ( ) ) ;
120
120
const response = NextResponse . rewrite ( rewrittenURL , {
@@ -163,7 +163,7 @@ function serveErrorResponse(error: Error) {
163
163
* - The request URL is matching `/url/:url`:
164
164
* URL is taken from the pathname.
165
165
*/
166
- function extractURL ( request : NextRequest ) : URLWithMode | null {
166
+ function getSiteURLFromRequest ( request : NextRequest ) : URLWithMode | null {
167
167
const xGitbookUrl = request . headers . get ( 'x-gitbook-url' ) ;
168
168
if ( xGitbookUrl ) {
169
169
return {
@@ -172,30 +172,45 @@ function extractURL(request: NextRequest): URLWithMode | null {
172
172
} ;
173
173
}
174
174
175
- const xForwardedHost = request . headers . get ( 'x-forwarded-host' ) ;
176
- // The x-forwarded-host is set by Vercel for all requests
177
- // so we ignore it if the hostname is the same as the instance one.
178
- if ( xForwardedHost && GITBOOK_URL && new URL ( GITBOOK_URL ) . host !== xForwardedHost ) {
179
- console . log ( 'xForwardedHost' , xForwardedHost , GITBOOK_URL , new URL ( GITBOOK_URL ) . host ) ;
180
- console . log ( 'process.env.VERCEL_URL' , process . env . VERCEL_URL ) ;
181
- console . log ( 'process.env.GITBOOK_URL' , process . env . GITBOOK_URL ) ;
175
+ const isMainHost =
176
+ ( GITBOOK_URL && request . nextUrl . host === new URL ( GITBOOK_URL ) . host ) ||
177
+ ( process . env . VERCEL_URL && request . nextUrl . host === new URL ( process . env . VERCEL_URL ) . host ) ;
178
+ const isAssetsHost =
179
+ GITBOOK_ASSETS_URL && request . nextUrl . host === new URL ( GITBOOK_ASSETS_URL ) . host ;
180
+
181
+ // /url/:url requests on the main host
182
+ const prefix = '/url/' ;
183
+ if ( isMainHost && request . nextUrl . pathname . startsWith ( prefix ) ) {
182
184
return {
183
185
url : appendQueryParams (
184
- new URL ( `https://${ xForwardedHost } ${ request . nextUrl . pathname } ` ) ,
186
+ new URL ( `https://${ request . nextUrl . pathname . slice ( prefix . length ) } ` ) ,
185
187
request . nextUrl . searchParams
186
188
) ,
187
- mode : 'url-host ' ,
189
+ mode : 'url' ,
188
190
} ;
189
191
}
190
192
191
- const prefix = '/url/' ;
192
- if ( request . nextUrl . pathname . startsWith ( prefix ) ) {
193
+ // Skip other requests to main hosts
194
+ if ( isMainHost || isAssetsHost ) {
195
+ return null ;
196
+ }
197
+
198
+ const xForwardedHost = request . headers . get ( 'x-forwarded-host' ) ;
199
+ // The x-forwarded-host is set by Vercel for all requests
200
+ // so we ignore it if the hostname is the same as the instance one.
201
+ if ( xForwardedHost ) {
202
+ console . log ( 'xForwardedHost' , xForwardedHost , request . nextUrl . host ) ;
203
+ console . log ( 'env' , {
204
+ VERCEL_URL : process . env . VERCEL_URL ,
205
+ GITBOOK_URL : GITBOOK_URL ,
206
+ GITBOOK_ASSETS_URL : GITBOOK_ASSETS_URL ,
207
+ } ) ;
193
208
return {
194
209
url : appendQueryParams (
195
- new URL ( `https://${ request . nextUrl . pathname . slice ( prefix . length ) } ` ) ,
210
+ new URL ( `https://${ xForwardedHost } ${ request . nextUrl . pathname } ` ) ,
196
211
request . nextUrl . searchParams
197
212
) ,
198
- mode : 'url' ,
213
+ mode : 'url-host ' ,
199
214
} ;
200
215
}
201
216
0 commit comments