Skip to content

Commit 5134d9e

Browse files
conico974Nicolas Dorseuil
andauthored
Avoid unnecessary proxy URL lookups (#3377)
Co-authored-by: Nicolas Dorseuil <[email protected]>
1 parent 6821fb2 commit 5134d9e

File tree

4 files changed

+80
-1
lines changed

4 files changed

+80
-1
lines changed

packages/gitbook/src/lib/data/urls.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,35 @@ describe('getURLLookupAlternatives', () => {
362362
],
363363
});
364364
});
365+
366+
it('should skip proxy root URLs', () => {
367+
expect(
368+
getURLLookupAlternatives(
369+
new URL('https://proxy.gitbook.site/sites/site_foo/hello/world')
370+
)
371+
).toEqual({
372+
revision: undefined,
373+
changeRequest: undefined,
374+
basePath: undefined,
375+
urls: [
376+
{
377+
url: 'https://proxy.gitbook.site/sites/site_foo',
378+
extraPath: 'hello/world',
379+
primary: false,
380+
},
381+
{
382+
url: 'https://proxy.gitbook.site/sites/site_foo/hello',
383+
extraPath: 'world',
384+
primary: false,
385+
},
386+
{
387+
url: 'https://proxy.gitbook.site/sites/site_foo/hello/world',
388+
extraPath: '',
389+
primary: true,
390+
},
391+
],
392+
});
393+
});
365394
});
366395

367396
describe('normalizeURL', () => {

packages/gitbook/src/lib/data/urls.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { isProxyRootRequest } from '../proxy';
2+
13
/**
24
* For a given GitBook URL, return a list of alternative URLs that could be matched against to lookup the content.
35
* The approach is optimized to aim at reusing cached lookup results as much as possible.
@@ -39,6 +41,11 @@ export function getURLLookupAlternatives(input: URL) {
3941
return;
4042
}
4143

44+
// We don't want to push the url if it is the root proxy URL (either https://proxy.gitbook.site or https://proxy.gitbook.site/sites)
45+
if (isProxyRootRequest(adding)) {
46+
return;
47+
}
48+
4249
alternatives.push({
4350
url: adding.toString(),
4451
extraPath,

packages/gitbook/src/lib/proxy.test.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it } from 'bun:test';
2-
import { getProxyRequestIdentifier, isProxyRequest } from './proxy';
2+
import { getProxyRequestIdentifier, isProxyRequest, isProxyRootRequest } from './proxy';
33

44
describe('isProxyRequest', () => {
55
it('should return true for proxy requests', () => {
@@ -19,3 +19,32 @@ describe('getProxyRequestIdentifier', () => {
1919
expect(getProxyRequestIdentifier(proxyRequestURL)).toBe('sites/site_foo');
2020
});
2121
});
22+
23+
describe('isProxyRootRequest', () => {
24+
it('should return true for root proxy requests', () => {
25+
const rootProxyRequestURL = new URL('https://proxy.gitbook.site/');
26+
expect(isProxyRootRequest(rootProxyRequestURL)).toBe(true);
27+
});
28+
29+
it('should return true for sites root proxy requests', () => {
30+
const sitesRootProxyRequestURL = new URL('https://proxy.gitbook.site/sites');
31+
expect(isProxyRootRequest(sitesRootProxyRequestURL)).toBe(true);
32+
});
33+
34+
it('should return true for sites root proxy requests with trailing slash', () => {
35+
const sitesRootProxyRequestURL = new URL('https://proxy.gitbook.site/sites/');
36+
expect(isProxyRootRequest(sitesRootProxyRequestURL)).toBe(true);
37+
});
38+
39+
it('should return false for non proxy requests', () => {
40+
const nonRootProxyRequestURL = new URL('https://example.com/docs/foo/hello/world');
41+
expect(isProxyRootRequest(nonRootProxyRequestURL)).toBe(false);
42+
});
43+
44+
it('should return false for non-root proxy requests', () => {
45+
const nonRootProxyRequestURL = new URL(
46+
'https://proxy.gitbook.site/sites/site_foo/hello/world'
47+
);
48+
expect(isProxyRootRequest(nonRootProxyRequestURL)).toBe(false);
49+
});
50+
});

packages/gitbook/src/lib/proxy.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,17 @@ export function getProxyRequestIdentifier(requestURL: URL): string {
1313
const pathname = requestURL.pathname.slice(1).split('/');
1414
return pathname.slice(0, 2).join('/');
1515
}
16+
17+
/**
18+
* Check if the request is for the root of the proxy site (i.e. https://proxy.gitbook.site/ or https://proxy.gitbook.site/sites).
19+
* @param requestURL The URL of the request to check if it is a proxy root request.
20+
*/
21+
export function isProxyRootRequest(requestURL: URL): boolean {
22+
// Check if the request is for the root of the proxy site
23+
return (
24+
isProxyRequest(requestURL) &&
25+
(requestURL.pathname === '/' ||
26+
requestURL.pathname === '/sites' ||
27+
requestURL.pathname === '/sites/')
28+
);
29+
}

0 commit comments

Comments
 (0)