Skip to content

Commit

Permalink
Fix image resizing in v2 when url is being passed as x-gitbook-url (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
SamyPesse authored Mar 4, 2025
1 parent bfcc119 commit 821899a
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions packages/gitbook-v2/src/middleware.ts
Original file line number Diff line number Diff line change
@@ -16,21 +16,21 @@ type URLWithMode = { url: URL; mode: 'url' | 'url-host' };

export async function middleware(request: NextRequest) {
try {
/**
* Serve image resizing requests (all requests containing `/~gitbook/image`).
* All URLs containing `/~gitbook/image` are rewritten to `/~gitbook/image`
* and serve from a single route handler.
*
* In GitBook v1: image resizing was done at the root of the hostname (docs.company.com/~gitbook/image)
* In GitBook v2: image resizing is done at the content level (docs.company.com/section/variant/~gitbook/image)
*/
if (request.nextUrl.pathname.endsWith('/~gitbook/image')) {
return serveResizedImage(request);
}

// Route all requests to a site
const extracted = extractURL(request);
if (extracted) {
/**
* Serve image resizing requests (all requests containing `/~gitbook/image`).
* All URLs containing `/~gitbook/image` are rewritten to `/~gitbook/image`
* and serve from a single route handler.
*
* In GitBook v1: image resizing was done at the root of the hostname (docs.company.com/~gitbook/image)
* In GitBook v2: image resizing is done at the content level (docs.company.com/section/variant/~gitbook/image)
*/
if (extracted.url.pathname.endsWith('/~gitbook/image')) {
return serveResizedImage(request);
}

return serveSiteByURL(request, extracted);
}

0 comments on commit 821899a

Please sign in to comment.