Skip to content

Commit bfcc119

Browse files
authored
Remove support for x-gitbook-host in v2 (#2918)
1 parent 76c7974 commit bfcc119

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

packages/gitbook-v2/src/app/~gitbook/revalidate/route.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export async function POST(req: NextRequest) {
2323
}
2424

2525
body.tags.forEach((tag) => {
26+
// biome-ignore lint/suspicious/noConsole: we want to log here
27+
console.log(`Revalidating tag: ${tag}`);
2628
revalidateTag(tag);
2729
});
2830

packages/gitbook-v2/src/middleware.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ export async function middleware(request: NextRequest) {
4545
* Serve site by URL.
4646
*/
4747
async function serveSiteByURL(request: NextRequest, urlWithMode: URLWithMode) {
48-
const dynamicHeaders = getDynamicHeaders(request);
4948
const { url, mode } = urlWithMode;
49+
const dynamicHeaders = getDynamicHeaders(url, request);
5050

5151
const result = await getPublishedContentByURL({
5252
url: url.toString(),
@@ -121,30 +121,24 @@ function serveErrorResponse(error: Error) {
121121
/**
122122
* The URL of the GitBook content can be passed in 3 different ways:
123123
* - 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.
125124
* - The request URL is matching `/url/:url`
126125
*/
127126
function extractURL(request: NextRequest): URLWithMode | null {
128127
const xGitbookUrl = request.headers.get('x-gitbook-url');
129128
if (xGitbookUrl) {
130129
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),
140131
mode: 'url-host',
141132
};
142133
}
143134

144135
const prefix = '/url/';
145136
if (request.nextUrl.pathname.startsWith(prefix)) {
146137
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+
),
148142
mode: 'url',
149143
};
150144
}
@@ -155,7 +149,7 @@ function extractURL(request: NextRequest): URLWithMode | null {
155149
/**
156150
* Evaluate if a request is dynamic or static.
157151
*/
158-
function getDynamicHeaders(_request: NextRequest): null | Record<string, string> {
152+
function getDynamicHeaders(_url: URL, _request: NextRequest): null | Record<string, string> {
159153
// TODO:
160154
// - check token in query string
161155
// - check token in cookies
@@ -185,3 +179,14 @@ function encodePathInSiteContent(rawPathname: string) {
185179
return encodeURIComponent(pathname || '/');
186180
}
187181
}
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

Comments
 (0)