Fix error when accessing some not found pages (#2751)

This commit is contained in:
Greg Bergé
2025-01-15 12:04:27 +01:00
committed by GitHub
parent 65cc4afb23
commit 142938401c
26 changed files with 42 additions and 31 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'gitbook': patch
---
Fix error when accessing some not found pages.
@@ -5,6 +5,7 @@ import ReactDOM from 'react-dom';
import { Card } from '@/components/primitives';
import { getEmbedByUrlInSpace, getEmbedByUrl } from '@/lib/api';
import { getContentSecurityPolicyNonce } from '@/lib/csp';
import { tcls } from '@/lib/tailwind';
import { BlockProps } from './Block';
@@ -13,8 +14,7 @@ import { IntegrationBlock } from './Integration';
export async function Embed(props: BlockProps<gitbookAPI.DocumentBlockEmbed>) {
const { block, context, ...otherProps } = props;
const headersList = await headers();
const nonce = headersList.get('x-nonce') || undefined;
const nonce = await getContentSecurityPolicyNonce();
ReactDOM.preload('https://cdn.iframe.ly/embed.js', { as: 'script', nonce });
+3 -4
View File
@@ -1,19 +1,18 @@
import { SpaceIntegrationScript } from '@gitbook/api';
import { merge } from 'content-security-policy-merger';
import { headers } from 'next/headers';
import { assert } from 'ts-essentials';
import { assetsDomain } from './assets';
import { filterOutNullable } from './typescript';
/**
* Get the current nonce for the current request.
*/
export async function getContentSecurityPolicyNonce(): Promise<string> {
const headersList = await headers();
const nonce = headersList.get('x-nonce');
if (!nonce) {
throw new Error('No nonce found in headers');
}
assert(nonce, 'x-nonce should be set in the headers by the middleware');
return nonce;
}
+1
View File
@@ -5,6 +5,7 @@ import {
RevisionPageGroup,
RevisionPageType,
} from '@gitbook/api';
import { headers } from 'next/headers';
export type AncestorRevisionPage = RevisionPageDocument | RevisionPageGroup;
+30 -23
View File
@@ -1,4 +1,5 @@
import { headers } from 'next/headers';
import { assert } from 'ts-essentials';
import { SiteContentPointer, SpaceContentPointer } from './api';
@@ -7,28 +8,34 @@ import { SiteContentPointer, SpaceContentPointer } from './api';
*/
export async function getSiteContentPointer(): Promise<SiteContentPointer> {
const headersList = await headers();
const spaceId = headersList.get('x-gitbook-content-space');
const siteId = headersList.get('x-gitbook-content-site');
const organizationId = headersList.get('x-gitbook-content-organization');
const siteSpaceId = headersList.get('x-gitbook-content-site-space');
const siteSectionId = headersList.get('x-gitbook-content-site-section');
const siteShareKey = headersList.get('x-gitbook-content-site-share-key');
if (!spaceId || !siteId || !organizationId) {
throw new Error(
'getSiteContentPointer is called outside the scope of a request processed by the middleware',
);
}
const spaceId = headersList.get('x-gitbook-content-space');
assert(spaceId, 'x-gitbook-content-space should be set in the headers by the middleware');
const siteId = headersList.get('x-gitbook-content-site');
assert(siteId, 'x-gitbook-content-site should be set in the headers by the middleware');
const organizationId = headersList.get('x-gitbook-content-organization');
assert(
organizationId,
'x-gitbook-content-organization should be set in the headers by the middleware',
);
const siteSectionId = headersList.get('x-gitbook-content-site-section') ?? undefined;
const siteSpaceId = headersList.get('x-gitbook-content-site-space') ?? undefined;
const siteShareKey = headersList.get('x-gitbook-content-site-share-key') ?? undefined;
const revisionId = headersList.get('x-gitbook-content-revision') ?? undefined;
const changeRequestId = headersList.get('x-gitbook-content-changerequest') ?? undefined;
const pointer: SiteContentPointer = {
siteId,
spaceId,
siteSectionId: siteSectionId ?? undefined,
siteSpaceId: siteSpaceId ?? undefined,
siteShareKey: siteShareKey ?? undefined,
organizationId,
revisionId: headersList.get('x-gitbook-content-revision') ?? undefined,
changeRequestId: headersList.get('x-gitbook-content-changerequest') ?? undefined,
siteSectionId,
siteSpaceId,
siteShareKey,
revisionId,
changeRequestId,
};
return pointer;
@@ -40,17 +47,17 @@ export async function getSiteContentPointer(): Promise<SiteContentPointer> {
*/
export async function getSpacePointer(): Promise<SpaceContentPointer> {
const headersList = await headers();
const spaceId = headersList.get('x-gitbook-content-space');
if (!spaceId) {
throw new Error(
'getSpacePointer is called outside the scope of a request processed by the middleware',
);
}
assert(spaceId, 'x-gitbook-content-space should be set in the headers by the middleware');
const revisionId = headersList.get('x-gitbook-content-revision') ?? undefined;
const changeRequestId = headersList.get('x-gitbook-content-changerequest') ?? undefined;
const pointer: SpaceContentPointer = {
spaceId,
revisionId: headersList.get('x-gitbook-content-revision') ?? undefined,
changeRequestId: headersList.get('x-gitbook-content-changerequest') ?? undefined,
revisionId,
changeRequestId,
};
return pointer;
+1 -2
View File
@@ -5,7 +5,6 @@ import jwt from 'jsonwebtoken';
import type { ResponseCookie } from 'next/dist/compiled/@edge-runtime/cookies';
import { NextResponse, NextRequest } from 'next/server';
import hash from 'object-hash';
import rison from 'rison';
import {
PublishedContentWithCache,
@@ -278,7 +277,7 @@ export async function middleware(request: NextRequest) {
headers.set('x-gitbook-visitor-token', resolved.visitorToken);
}
const target = new URL(rewritePathname, request.nextUrl.toString());
const target = new URL(joinPath('/middleware', rewritePathname), request.nextUrl.toString());
target.search = url.search;
const response = writeCookies(