mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-16 15:45:13 +00:00
Skip requests to assets/main/vercel hosts in middleware (#2927)
This commit is contained in:
@@ -8,7 +8,7 @@ import { removeLeadingSlash, removeTrailingSlash } from '@/lib/paths';
|
||||
import { getResponseCookiesForVisitorAuth, getVisitorToken } from '@/lib/visitor-token';
|
||||
import { serveResizedImage } from '@/routes/image';
|
||||
import { getPublishedContentByURL } from '@v2/lib/data';
|
||||
import { GITBOOK_URL } from '@v2/lib/env';
|
||||
import { GITBOOK_ASSETS_URL, GITBOOK_URL } from '@v2/lib/env';
|
||||
import { MiddlewareHeaders } from '@v2/lib/middleware';
|
||||
|
||||
export const config = {
|
||||
@@ -20,7 +20,7 @@ type URLWithMode = { url: URL; mode: 'url' | 'url-host' };
|
||||
export async function middleware(request: NextRequest) {
|
||||
try {
|
||||
// Route all requests to a site
|
||||
const extracted = extractURL(request);
|
||||
const extracted = getSiteURLFromRequest(request);
|
||||
if (extracted) {
|
||||
/**
|
||||
* Serve image resizing requests (all requests containing `/~gitbook/image`).
|
||||
@@ -114,7 +114,7 @@ async function serveSiteByURL(request: NextRequest, urlWithMode: URLWithMode) {
|
||||
encodePathInSiteContent(data.pathname),
|
||||
].join('/');
|
||||
|
||||
console.log(`rewriting to ${route}`);
|
||||
console.log(`rewriting ${request.nextUrl.toString()} to ${route}`);
|
||||
|
||||
const rewrittenURL = new URL(`/${route}`, request.nextUrl.toString());
|
||||
const response = NextResponse.rewrite(rewrittenURL, {
|
||||
@@ -163,7 +163,7 @@ function serveErrorResponse(error: Error) {
|
||||
* - The request URL is matching `/url/:url`:
|
||||
* URL is taken from the pathname.
|
||||
*/
|
||||
function extractURL(request: NextRequest): URLWithMode | null {
|
||||
function getSiteURLFromRequest(request: NextRequest): URLWithMode | null {
|
||||
const xGitbookUrl = request.headers.get('x-gitbook-url');
|
||||
if (xGitbookUrl) {
|
||||
return {
|
||||
@@ -172,24 +172,15 @@ function extractURL(request: NextRequest): URLWithMode | null {
|
||||
};
|
||||
}
|
||||
|
||||
const xForwardedHost = request.headers.get('x-forwarded-host');
|
||||
// The x-forwarded-host is set by Vercel for all requests
|
||||
// so we ignore it if the hostname is the same as the instance one.
|
||||
if (xForwardedHost && GITBOOK_URL && new URL(GITBOOK_URL).host !== xForwardedHost) {
|
||||
console.log('xForwardedHost', xForwardedHost, GITBOOK_URL, new URL(GITBOOK_URL).host);
|
||||
console.log('process.env.VERCEL_URL', process.env.VERCEL_URL);
|
||||
console.log('process.env.GITBOOK_URL', process.env.GITBOOK_URL);
|
||||
return {
|
||||
url: appendQueryParams(
|
||||
new URL(`https://${xForwardedHost}${request.nextUrl.pathname}`),
|
||||
request.nextUrl.searchParams
|
||||
),
|
||||
mode: 'url-host',
|
||||
};
|
||||
}
|
||||
const isMainHost =
|
||||
(GITBOOK_URL && request.nextUrl.host === new URL(GITBOOK_URL).host) ||
|
||||
(process.env.VERCEL_URL && request.nextUrl.host === new URL(process.env.VERCEL_URL).host);
|
||||
const isAssetsHost =
|
||||
GITBOOK_ASSETS_URL && request.nextUrl.host === new URL(GITBOOK_ASSETS_URL).host;
|
||||
|
||||
// /url/:url requests on the main host
|
||||
const prefix = '/url/';
|
||||
if (request.nextUrl.pathname.startsWith(prefix)) {
|
||||
if (isMainHost && request.nextUrl.pathname.startsWith(prefix)) {
|
||||
return {
|
||||
url: appendQueryParams(
|
||||
new URL(`https://${request.nextUrl.pathname.slice(prefix.length)}`),
|
||||
@@ -199,6 +190,30 @@ function extractURL(request: NextRequest): URLWithMode | null {
|
||||
};
|
||||
}
|
||||
|
||||
// Skip other requests to main hosts
|
||||
if (isMainHost || isAssetsHost) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const xForwardedHost = request.headers.get('x-forwarded-host');
|
||||
// The x-forwarded-host is set by Vercel for all requests
|
||||
// so we ignore it if the hostname is the same as the instance one.
|
||||
if (xForwardedHost) {
|
||||
console.log('xForwardedHost', xForwardedHost, request.nextUrl.host);
|
||||
console.log('env', {
|
||||
VERCEL_URL: process.env.VERCEL_URL,
|
||||
GITBOOK_URL: GITBOOK_URL,
|
||||
GITBOOK_ASSETS_URL: GITBOOK_ASSETS_URL,
|
||||
});
|
||||
return {
|
||||
url: appendQueryParams(
|
||||
new URL(`https://${xForwardedHost}${request.nextUrl.pathname}`),
|
||||
request.nextUrl.searchParams
|
||||
),
|
||||
mode: 'url-host',
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user