Skip to content

Commit 1429384

Browse files
authored
Fix error when accessing some not found pages (#2751)
1 parent 65cc4af commit 1429384

File tree

26 files changed

+39
-28
lines changed

26 files changed

+39
-28
lines changed

.changeset/thirty-berries-bow.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'gitbook': patch
3+
---
4+
5+
Fix error when accessing some not found pages.

packages/gitbook/src/components/DocumentView/Embed.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import ReactDOM from 'react-dom';
55

66
import { Card } from '@/components/primitives';
77
import { getEmbedByUrlInSpace, getEmbedByUrl } from '@/lib/api';
8+
import { getContentSecurityPolicyNonce } from '@/lib/csp';
89
import { tcls } from '@/lib/tailwind';
910

1011
import { BlockProps } from './Block';
@@ -13,8 +14,7 @@ import { IntegrationBlock } from './Integration';
1314

1415
export async function Embed(props: BlockProps<gitbookAPI.DocumentBlockEmbed>) {
1516
const { block, context, ...otherProps } = props;
16-
const headersList = await headers();
17-
const nonce = headersList.get('x-nonce') || undefined;
17+
const nonce = await getContentSecurityPolicyNonce();
1818

1919
ReactDOM.preload('https://cdn.iframe.ly/embed.js', { as: 'script', nonce });
2020

packages/gitbook/src/lib/csp.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
import { SpaceIntegrationScript } from '@gitbook/api';
22
import { merge } from 'content-security-policy-merger';
33
import { headers } from 'next/headers';
4+
import { assert } from 'ts-essentials';
45

56
import { assetsDomain } from './assets';
67
import { filterOutNullable } from './typescript';
8+
79
/**
810
* Get the current nonce for the current request.
911
*/
1012
export async function getContentSecurityPolicyNonce(): Promise<string> {
1113
const headersList = await headers();
1214
const nonce = headersList.get('x-nonce');
13-
if (!nonce) {
14-
throw new Error('No nonce found in headers');
15-
}
16-
15+
assert(nonce, 'x-nonce should be set in the headers by the middleware');
1716
return nonce;
1817
}
1918

packages/gitbook/src/lib/pages.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
RevisionPageGroup,
66
RevisionPageType,
77
} from '@gitbook/api';
8+
import { headers } from 'next/headers';
89

910
export type AncestorRevisionPage = RevisionPageDocument | RevisionPageGroup;
1011

packages/gitbook/src/lib/pointer.ts

+27-20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { headers } from 'next/headers';
2+
import { assert } from 'ts-essentials';
23

34
import { SiteContentPointer, SpaceContentPointer } from './api';
45

@@ -7,28 +8,34 @@ import { SiteContentPointer, SpaceContentPointer } from './api';
78
*/
89
export async function getSiteContentPointer(): Promise<SiteContentPointer> {
910
const headersList = await headers();
11+
1012
const spaceId = headersList.get('x-gitbook-content-space');
13+
assert(spaceId, 'x-gitbook-content-space should be set in the headers by the middleware');
14+
1115
const siteId = headersList.get('x-gitbook-content-site');
16+
assert(siteId, 'x-gitbook-content-site should be set in the headers by the middleware');
17+
1218
const organizationId = headersList.get('x-gitbook-content-organization');
13-
const siteSpaceId = headersList.get('x-gitbook-content-site-space');
14-
const siteSectionId = headersList.get('x-gitbook-content-site-section');
15-
const siteShareKey = headersList.get('x-gitbook-content-site-share-key');
19+
assert(
20+
organizationId,
21+
'x-gitbook-content-organization should be set in the headers by the middleware',
22+
);
1623

17-
if (!spaceId || !siteId || !organizationId) {
18-
throw new Error(
19-
'getSiteContentPointer is called outside the scope of a request processed by the middleware',
20-
);
21-
}
24+
const siteSectionId = headersList.get('x-gitbook-content-site-section') ?? undefined;
25+
const siteSpaceId = headersList.get('x-gitbook-content-site-space') ?? undefined;
26+
const siteShareKey = headersList.get('x-gitbook-content-site-share-key') ?? undefined;
27+
const revisionId = headersList.get('x-gitbook-content-revision') ?? undefined;
28+
const changeRequestId = headersList.get('x-gitbook-content-changerequest') ?? undefined;
2229

2330
const pointer: SiteContentPointer = {
2431
siteId,
2532
spaceId,
26-
siteSectionId: siteSectionId ?? undefined,
27-
siteSpaceId: siteSpaceId ?? undefined,
28-
siteShareKey: siteShareKey ?? undefined,
2933
organizationId,
30-
revisionId: headersList.get('x-gitbook-content-revision') ?? undefined,
31-
changeRequestId: headersList.get('x-gitbook-content-changerequest') ?? undefined,
34+
siteSectionId,
35+
siteSpaceId,
36+
siteShareKey,
37+
revisionId,
38+
changeRequestId,
3239
};
3340

3441
return pointer;
@@ -40,17 +47,17 @@ export async function getSiteContentPointer(): Promise<SiteContentPointer> {
4047
*/
4148
export async function getSpacePointer(): Promise<SpaceContentPointer> {
4249
const headersList = await headers();
50+
4351
const spaceId = headersList.get('x-gitbook-content-space');
44-
if (!spaceId) {
45-
throw new Error(
46-
'getSpacePointer is called outside the scope of a request processed by the middleware',
47-
);
48-
}
52+
assert(spaceId, 'x-gitbook-content-space should be set in the headers by the middleware');
53+
54+
const revisionId = headersList.get('x-gitbook-content-revision') ?? undefined;
55+
const changeRequestId = headersList.get('x-gitbook-content-changerequest') ?? undefined;
4956

5057
const pointer: SpaceContentPointer = {
5158
spaceId,
52-
revisionId: headersList.get('x-gitbook-content-revision') ?? undefined,
53-
changeRequestId: headersList.get('x-gitbook-content-changerequest') ?? undefined,
59+
revisionId,
60+
changeRequestId,
5461
};
5562

5663
return pointer;

packages/gitbook/src/middleware.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import jwt from 'jsonwebtoken';
55
import type { ResponseCookie } from 'next/dist/compiled/@edge-runtime/cookies';
66
import { NextResponse, NextRequest } from 'next/server';
77
import hash from 'object-hash';
8-
import rison from 'rison';
98

109
import {
1110
PublishedContentWithCache,
@@ -278,7 +277,7 @@ export async function middleware(request: NextRequest) {
278277
headers.set('x-gitbook-visitor-token', resolved.visitorToken);
279278
}
280279

281-
const target = new URL(rewritePathname, request.nextUrl.toString());
280+
const target = new URL(joinPath('/middleware', rewritePathname), request.nextUrl.toString());
282281
target.search = url.search;
283282

284283
const response = writeCookies(

0 commit comments

Comments
 (0)