Skip to content

Commit 6f8ecd6

Browse files
authored
Use more strict context ID for images during preview (#3261)
1 parent fa3eb07 commit 6f8ecd6

File tree

8 files changed

+59
-3
lines changed

8 files changed

+59
-3
lines changed

packages/gitbook-v2/src/lib/data/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@ export * from './pages';
44
export * from './urls';
55
export * from './errors';
66
export * from './lookup';
7-
export * from './proxy';
87
export * from './visitor';

packages/gitbook-v2/src/lib/data/visitor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { withLeadingSlash, withTrailingSlash } from '@/lib/paths';
22
import type { PublishedSiteContent } from '@gitbook/api';
3-
import { getProxyRequestIdentifier, isProxyRequest } from './proxy';
3+
import { getProxyRequestIdentifier, isProxyRequest } from '@v2/lib/proxy';
44

55
/**
66
* Get the appropriate base path for the visitor authentication cookie.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { describe, expect, it } from 'bun:test';
2+
import { getImageResizingContextId } from './getImageResizingContextId';
3+
4+
describe('getImageResizingContextId', () => {
5+
it('should return proxy identifier for proxy requests', () => {
6+
const proxyRequestURL = new URL('https://proxy.gitbook.site/sites/site_foo/hello/world');
7+
expect(getImageResizingContextId(proxyRequestURL)).toBe('sites/site_foo');
8+
});
9+
10+
it('should return preview identifier for preview requests', () => {
11+
const previewRequestURL = new URL('https://preview/site_foo/hello/world');
12+
expect(getImageResizingContextId(previewRequestURL)).toBe('site_foo');
13+
});
14+
15+
it('should return host for regular requests', () => {
16+
const regularRequestURL = new URL('https://example.com/docs/foo/hello/world');
17+
expect(getImageResizingContextId(regularRequestURL)).toBe('example.com');
18+
});
19+
});

packages/gitbook-v2/src/lib/images/getImageResizingContextId.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { getProxyRequestIdentifier, isProxyRequest } from '../data';
1+
import { getPreviewRequestIdentifier, isPreviewRequest } from '@v2/lib/preview';
2+
import { getProxyRequestIdentifier, isProxyRequest } from '@v2/lib/proxy';
23

34
/**
45
* Get the site identifier to use for image resizing for an incoming request.
@@ -8,6 +9,9 @@ export function getImageResizingContextId(url: URL): string {
89
if (isProxyRequest(url)) {
910
return getProxyRequestIdentifier(url);
1011
}
12+
if (isPreviewRequest(url)) {
13+
return getPreviewRequestIdentifier(url);
14+
}
1115

1216
return url.host;
1317
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { describe, expect, it } from 'bun:test';
2+
import { getPreviewRequestIdentifier, isPreviewRequest } from './preview';
3+
4+
describe('isPreviewRequest', () => {
5+
it('should return true for preview requests', () => {
6+
const previewRequestURL = new URL('https://preview/site_foo/hello/world');
7+
expect(isPreviewRequest(previewRequestURL)).toBe(true);
8+
});
9+
10+
it('should return false for non-preview requests', () => {
11+
const nonPreviewRequestURL = new URL('https://example.com/docs/foo/hello/world');
12+
expect(isPreviewRequest(nonPreviewRequestURL)).toBe(false);
13+
});
14+
});
15+
16+
describe('getPreviewRequestIdentifier', () => {
17+
it('should return the correct identifier for preview requests', () => {
18+
const previewRequestURL = new URL('https://preview/site_foo/hello/world');
19+
expect(getPreviewRequestIdentifier(previewRequestURL)).toBe('site_foo');
20+
});
21+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Check if the request to the site is a preview request.
3+
*/
4+
export function isPreviewRequest(requestURL: URL): boolean {
5+
return requestURL.host === 'preview';
6+
}
7+
8+
export function getPreviewRequestIdentifier(requestURL: URL): string {
9+
// For preview requests, we extract the site ID from the pathname
10+
// e.g. https://preview/site_id/...
11+
const pathname = requestURL.pathname.slice(1).split('/');
12+
return pathname[0];
13+
}

0 commit comments

Comments
 (0)