@@ -45,8 +45,8 @@ export async function middleware(request: NextRequest) {
45
45
* Serve site by URL.
46
46
*/
47
47
async function serveSiteByURL ( request : NextRequest , urlWithMode : URLWithMode ) {
48
- const dynamicHeaders = getDynamicHeaders ( request ) ;
49
48
const { url, mode } = urlWithMode ;
49
+ const dynamicHeaders = getDynamicHeaders ( url , request ) ;
50
50
51
51
const result = await getPublishedContentByURL ( {
52
52
url : url . toString ( ) ,
@@ -121,30 +121,24 @@ function serveErrorResponse(error: Error) {
121
121
/**
122
122
* The URL of the GitBook content can be passed in 3 different ways:
123
123
* - The request URL is in the `X-GitBook-URL` header.
124
- * - Hostname is in the `X-GitBook-Host` header and the pathname is the path in the request URL.
125
124
* - The request URL is matching `/url/:url`
126
125
*/
127
126
function extractURL ( request : NextRequest ) : URLWithMode | null {
128
127
const xGitbookUrl = request . headers . get ( 'x-gitbook-url' ) ;
129
128
if ( xGitbookUrl ) {
130
129
return {
131
- url : new URL ( xGitbookUrl ) ,
132
- mode : 'url-host' ,
133
- } ;
134
- }
135
-
136
- const xGitbookHost = request . headers . get ( 'x-gitbook-host' ) ;
137
- if ( xGitbookHost ) {
138
- return {
139
- url : new URL ( `https://${ xGitbookHost } ${ request . nextUrl . pathname } ` ) ,
130
+ url : appendQueryParams ( new URL ( xGitbookUrl ) , request . nextUrl . searchParams ) ,
140
131
mode : 'url-host' ,
141
132
} ;
142
133
}
143
134
144
135
const prefix = '/url/' ;
145
136
if ( request . nextUrl . pathname . startsWith ( prefix ) ) {
146
137
return {
147
- url : new URL ( `https://${ request . nextUrl . pathname . slice ( prefix . length ) } ` ) ,
138
+ url : appendQueryParams (
139
+ new URL ( `https://${ request . nextUrl . pathname . slice ( prefix . length ) } ` ) ,
140
+ request . nextUrl . searchParams
141
+ ) ,
148
142
mode : 'url' ,
149
143
} ;
150
144
}
@@ -155,7 +149,7 @@ function extractURL(request: NextRequest): URLWithMode | null {
155
149
/**
156
150
* Evaluate if a request is dynamic or static.
157
151
*/
158
- function getDynamicHeaders ( _request : NextRequest ) : null | Record < string , string > {
152
+ function getDynamicHeaders ( _url : URL , _request : NextRequest ) : null | Record < string , string > {
159
153
// TODO:
160
154
// - check token in query string
161
155
// - check token in cookies
@@ -185,3 +179,14 @@ function encodePathInSiteContent(rawPathname: string) {
185
179
return encodeURIComponent ( pathname || '/' ) ;
186
180
}
187
181
}
182
+
183
+ /**
184
+ * Append all the query params from a URL to another URL.
185
+ */
186
+ function appendQueryParams ( url : URL , from : URLSearchParams ) {
187
+ for ( const [ key , value ] of from . entries ( ) ) {
188
+ url . searchParams . set ( key , value ) ;
189
+ }
190
+
191
+ return url ;
192
+ }
0 commit comments