Skip to content

Commit c7bbeab

Browse files
committed
Fix URLs on GitBook v1
1 parent 373f18f commit c7bbeab

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

packages/gitbook-v2/src/lib/links.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ export function createLinker(
9393
},
9494

9595
toAbsoluteURL(absolutePath: string): string {
96+
if (absolutePath.startsWith('http://') || absolutePath.startsWith('https://')) {
97+
return absolutePath;
98+
}
99+
96100
if (!servedOn.host) {
97101
return absolutePath;
98102
}

packages/gitbook/src/lib/v1.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,21 @@ export async function getV1BaseContext(): Promise<GitBookBaseContext> {
4848
const host = await getHost();
4949
const basePath = await getBasePath();
5050
const siteBasePath = await getSiteBasePath();
51+
const protocol = await getProtocol();
5152

5253
const linker = createLinker({
54+
protocol,
5355
host,
5456
spaceBasePath: basePath,
5557
siteBasePath: siteBasePath,
5658
});
5759

5860
// On V1, we use hard-navigation between different spaces because of layout issues
59-
linker.toLinkForContent = (url) => {
60-
return url;
61+
linker.toLinkForContent = (rawURL) => {
62+
const urlObject = new URL(rawURL);
63+
return linker.toAbsoluteURL(
64+
linker.toPathInSite(`${urlObject.pathname}${urlObject.search}`)
65+
);
6166
};
6267

6368
const dataFetcher = getDataFetcherV1();
@@ -66,6 +71,7 @@ export async function getV1BaseContext(): Promise<GitBookBaseContext> {
6671
imagesContextId: host,
6772
// In V1, we always resize at the top level of the hostname, not relative to the content.
6873
linker: createLinker({
74+
protocol,
6975
host,
7076
spaceBasePath: '/',
7177
siteBasePath: '/',
@@ -401,6 +407,19 @@ export function getSitePointerFromContext(context: GitBookSiteContext): SiteCont
401407
};
402408
}
403409

410+
/**
411+
* Return the protocol for the current request.
412+
*/
413+
async function getProtocol(): Promise<string> {
414+
assertIsNotV2();
415+
const headersList = await headers();
416+
const protocol = headersList.get('x-forwarded-proto') ?? 'https';
417+
if (protocol.endsWith(':')) {
418+
return protocol;
419+
}
420+
return `${protocol}:`;
421+
}
422+
404423
/**
405424
* Return the base path for the current request.
406425
* The value will start and finish with /

packages/gitbook/src/middleware.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ export async function middleware(request: NextRequest) {
156156
if (contextId) {
157157
headers.set('x-gitbook-token-context', contextId);
158158
}
159+
if (inputURL.protocol === 'http:') {
160+
headers.set('x-forwarded-proto', 'http');
161+
}
159162
headers.set('x-gitbook-mode', mode);
160163
headers.set('x-gitbook-origin-basepath', originBasePath);
161164
headers.set(

0 commit comments

Comments
 (0)