Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error when accessing some not found pages #2751

Merged
merged 3 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/thirty-berries-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'gitbook': patch
---

Fix error when accessing some not found pages.
4 changes: 2 additions & 2 deletions packages/gitbook/src/components/DocumentView/Embed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 });

Expand Down
7 changes: 3 additions & 4 deletions packages/gitbook/src/lib/csp.ts
Original file line number Diff line number Diff line change
@@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions packages/gitbook/src/lib/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
RevisionPageGroup,
RevisionPageType,
} from '@gitbook/api';
import { headers } from 'next/headers';

export type AncestorRevisionPage = RevisionPageDocument | RevisionPageGroup;

Expand Down
47 changes: 27 additions & 20 deletions packages/gitbook/src/lib/pointer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { headers } from 'next/headers';
import { assert } from 'ts-essentials';

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

Expand All @@ -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');
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');
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');
assert(
organizationId,
'x-gitbook-content-organization should be set in the headers by the middleware',
);

if (!spaceId || !siteId || !organizationId) {
throw new Error(
'getSiteContentPointer is called outside the scope of a request processed 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;
Expand All @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions packages/gitbook/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
Loading