Skip to content

Commit 6d77b64

Browse files
authored
Skip requests to assets/main/vercel hosts in middleware (#2927)
1 parent 5907bd9 commit 6d77b64

File tree

1 file changed

+32
-17
lines changed

1 file changed

+32
-17
lines changed

packages/gitbook-v2/src/middleware.ts

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { removeLeadingSlash, removeTrailingSlash } from '@/lib/paths';
88
import { getResponseCookiesForVisitorAuth, getVisitorToken } from '@/lib/visitor-token';
99
import { serveResizedImage } from '@/routes/image';
1010
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';
1212
import { MiddlewareHeaders } from '@v2/lib/middleware';
1313

1414
export const config = {
@@ -20,7 +20,7 @@ type URLWithMode = { url: URL; mode: 'url' | 'url-host' };
2020
export async function middleware(request: NextRequest) {
2121
try {
2222
// Route all requests to a site
23-
const extracted = extractURL(request);
23+
const extracted = getSiteURLFromRequest(request);
2424
if (extracted) {
2525
/**
2626
* Serve image resizing requests (all requests containing `/~gitbook/image`).
@@ -114,7 +114,7 @@ async function serveSiteByURL(request: NextRequest, urlWithMode: URLWithMode) {
114114
encodePathInSiteContent(data.pathname),
115115
].join('/');
116116

117-
console.log(`rewriting to ${route}`);
117+
console.log(`rewriting ${request.nextUrl.toString()} to ${route}`);
118118

119119
const rewrittenURL = new URL(`/${route}`, request.nextUrl.toString());
120120
const response = NextResponse.rewrite(rewrittenURL, {
@@ -163,7 +163,7 @@ function serveErrorResponse(error: Error) {
163163
* - The request URL is matching `/url/:url`:
164164
* URL is taken from the pathname.
165165
*/
166-
function extractURL(request: NextRequest): URLWithMode | null {
166+
function getSiteURLFromRequest(request: NextRequest): URLWithMode | null {
167167
const xGitbookUrl = request.headers.get('x-gitbook-url');
168168
if (xGitbookUrl) {
169169
return {
@@ -172,30 +172,45 @@ function extractURL(request: NextRequest): URLWithMode | null {
172172
};
173173
}
174174

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)) {
182184
return {
183185
url: appendQueryParams(
184-
new URL(`https://${xForwardedHost}${request.nextUrl.pathname}`),
186+
new URL(`https://${request.nextUrl.pathname.slice(prefix.length)}`),
185187
request.nextUrl.searchParams
186188
),
187-
mode: 'url-host',
189+
mode: 'url',
188190
};
189191
}
190192

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+
});
193208
return {
194209
url: appendQueryParams(
195-
new URL(`https://${request.nextUrl.pathname.slice(prefix.length)}`),
210+
new URL(`https://${xForwardedHost}${request.nextUrl.pathname}`),
196211
request.nextUrl.searchParams
197212
),
198-
mode: 'url',
213+
mode: 'url-host',
199214
};
200215
}
201216

0 commit comments

Comments
 (0)