diff --git a/.changeset/thirty-berries-bow.md b/.changeset/thirty-berries-bow.md new file mode 100644 index 0000000000..a2ef63edb2 --- /dev/null +++ b/.changeset/thirty-berries-bow.md @@ -0,0 +1,5 @@ +--- +'gitbook': patch +--- + +Fix error when accessing some not found pages. diff --git a/packages/gitbook/src/app/(site)/(content)/ClientContexts.tsx b/packages/gitbook/src/app/middleware/(site)/(content)/ClientContexts.tsx similarity index 100% rename from packages/gitbook/src/app/(site)/(content)/ClientContexts.tsx rename to packages/gitbook/src/app/middleware/(site)/(content)/ClientContexts.tsx diff --git a/packages/gitbook/src/app/(site)/(content)/RocketLoaderDetector.tsx b/packages/gitbook/src/app/middleware/(site)/(content)/RocketLoaderDetector.tsx similarity index 100% rename from packages/gitbook/src/app/(site)/(content)/RocketLoaderDetector.tsx rename to packages/gitbook/src/app/middleware/(site)/(content)/RocketLoaderDetector.tsx diff --git a/packages/gitbook/src/app/(site)/(content)/[[...pathname]]/PageClientLayout.tsx b/packages/gitbook/src/app/middleware/(site)/(content)/[[...pathname]]/PageClientLayout.tsx similarity index 100% rename from packages/gitbook/src/app/(site)/(content)/[[...pathname]]/PageClientLayout.tsx rename to packages/gitbook/src/app/middleware/(site)/(content)/[[...pathname]]/PageClientLayout.tsx diff --git a/packages/gitbook/src/app/(site)/(content)/[[...pathname]]/loading.tsx b/packages/gitbook/src/app/middleware/(site)/(content)/[[...pathname]]/loading.tsx similarity index 100% rename from packages/gitbook/src/app/(site)/(content)/[[...pathname]]/loading.tsx rename to packages/gitbook/src/app/middleware/(site)/(content)/[[...pathname]]/loading.tsx diff --git a/packages/gitbook/src/app/(site)/(content)/[[...pathname]]/not-found.tsx b/packages/gitbook/src/app/middleware/(site)/(content)/[[...pathname]]/not-found.tsx similarity index 100% rename from packages/gitbook/src/app/(site)/(content)/[[...pathname]]/not-found.tsx rename to packages/gitbook/src/app/middleware/(site)/(content)/[[...pathname]]/not-found.tsx diff --git a/packages/gitbook/src/app/(site)/(content)/[[...pathname]]/page.tsx b/packages/gitbook/src/app/middleware/(site)/(content)/[[...pathname]]/page.tsx similarity index 100% rename from packages/gitbook/src/app/(site)/(content)/[[...pathname]]/page.tsx rename to packages/gitbook/src/app/middleware/(site)/(content)/[[...pathname]]/page.tsx diff --git a/packages/gitbook/src/app/(site)/(content)/layout.tsx b/packages/gitbook/src/app/middleware/(site)/(content)/layout.tsx similarity index 100% rename from packages/gitbook/src/app/(site)/(content)/layout.tsx rename to packages/gitbook/src/app/middleware/(site)/(content)/layout.tsx diff --git a/packages/gitbook/src/app/(site)/(core)/robots.txt/route.ts b/packages/gitbook/src/app/middleware/(site)/(core)/robots.txt/route.ts similarity index 100% rename from packages/gitbook/src/app/(site)/(core)/robots.txt/route.ts rename to packages/gitbook/src/app/middleware/(site)/(core)/robots.txt/route.ts diff --git a/packages/gitbook/src/app/(site)/(core)/sitemap.xml/route.ts b/packages/gitbook/src/app/middleware/(site)/(core)/sitemap.xml/route.ts similarity index 100% rename from packages/gitbook/src/app/(site)/(core)/sitemap.xml/route.ts rename to packages/gitbook/src/app/middleware/(site)/(core)/sitemap.xml/route.ts diff --git a/packages/gitbook/src/app/(site)/(core)/~gitbook/icon/route.tsx b/packages/gitbook/src/app/middleware/(site)/(core)/~gitbook/icon/route.tsx similarity index 100% rename from packages/gitbook/src/app/(site)/(core)/~gitbook/icon/route.tsx rename to packages/gitbook/src/app/middleware/(site)/(core)/~gitbook/icon/route.tsx diff --git a/packages/gitbook/src/app/(site)/(core)/~gitbook/ogimage/[pageId]/route.tsx b/packages/gitbook/src/app/middleware/(site)/(core)/~gitbook/ogimage/[pageId]/route.tsx similarity index 100% rename from packages/gitbook/src/app/(site)/(core)/~gitbook/ogimage/[pageId]/route.tsx rename to packages/gitbook/src/app/middleware/(site)/(core)/~gitbook/ogimage/[pageId]/route.tsx diff --git a/packages/gitbook/src/app/(site)/error.tsx b/packages/gitbook/src/app/middleware/(site)/error.tsx similarity index 100% rename from packages/gitbook/src/app/(site)/error.tsx rename to packages/gitbook/src/app/middleware/(site)/error.tsx diff --git a/packages/gitbook/src/app/(site)/fetch.ts b/packages/gitbook/src/app/middleware/(site)/fetch.ts similarity index 100% rename from packages/gitbook/src/app/(site)/fetch.ts rename to packages/gitbook/src/app/middleware/(site)/fetch.ts diff --git a/packages/gitbook/src/app/(site)/layout.tsx b/packages/gitbook/src/app/middleware/(site)/layout.tsx similarity index 100% rename from packages/gitbook/src/app/(site)/layout.tsx rename to packages/gitbook/src/app/middleware/(site)/layout.tsx diff --git a/packages/gitbook/src/app/(space)/~gitbook/pdf/PageControlButtons.tsx b/packages/gitbook/src/app/middleware/(space)/~gitbook/pdf/PageControlButtons.tsx similarity index 100% rename from packages/gitbook/src/app/(space)/~gitbook/pdf/PageControlButtons.tsx rename to packages/gitbook/src/app/middleware/(space)/~gitbook/pdf/PageControlButtons.tsx diff --git a/packages/gitbook/src/app/(space)/~gitbook/pdf/PrintButton.tsx b/packages/gitbook/src/app/middleware/(space)/~gitbook/pdf/PrintButton.tsx similarity index 100% rename from packages/gitbook/src/app/(space)/~gitbook/pdf/PrintButton.tsx rename to packages/gitbook/src/app/middleware/(space)/~gitbook/pdf/PrintButton.tsx diff --git a/packages/gitbook/src/app/(space)/~gitbook/pdf/layout.tsx b/packages/gitbook/src/app/middleware/(space)/~gitbook/pdf/layout.tsx similarity index 100% rename from packages/gitbook/src/app/(space)/~gitbook/pdf/layout.tsx rename to packages/gitbook/src/app/middleware/(space)/~gitbook/pdf/layout.tsx diff --git a/packages/gitbook/src/app/(space)/~gitbook/pdf/page.tsx b/packages/gitbook/src/app/middleware/(space)/~gitbook/pdf/page.tsx similarity index 100% rename from packages/gitbook/src/app/(space)/~gitbook/pdf/page.tsx rename to packages/gitbook/src/app/middleware/(space)/~gitbook/pdf/page.tsx diff --git a/packages/gitbook/src/app/(space)/~gitbook/pdf/pdf.css b/packages/gitbook/src/app/middleware/(space)/~gitbook/pdf/pdf.css similarity index 100% rename from packages/gitbook/src/app/(space)/~gitbook/pdf/pdf.css rename to packages/gitbook/src/app/middleware/(space)/~gitbook/pdf/pdf.css diff --git a/packages/gitbook/src/app/(space)/~gitbook/pdf/pointer.ts b/packages/gitbook/src/app/middleware/(space)/~gitbook/pdf/pointer.ts similarity index 100% rename from packages/gitbook/src/app/(space)/~gitbook/pdf/pointer.ts rename to packages/gitbook/src/app/middleware/(space)/~gitbook/pdf/pointer.ts diff --git a/packages/gitbook/src/components/DocumentView/Embed.tsx b/packages/gitbook/src/components/DocumentView/Embed.tsx index f07a08d421..77a2dda90b 100644 --- a/packages/gitbook/src/components/DocumentView/Embed.tsx +++ b/packages/gitbook/src/components/DocumentView/Embed.tsx @@ -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) { 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 }); diff --git a/packages/gitbook/src/lib/csp.ts b/packages/gitbook/src/lib/csp.ts index 663a2a14ac..660e557ad3 100644 --- a/packages/gitbook/src/lib/csp.ts +++ b/packages/gitbook/src/lib/csp.ts @@ -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 { 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; } diff --git a/packages/gitbook/src/lib/pages.ts b/packages/gitbook/src/lib/pages.ts index d6e485d9b9..991c40fa36 100644 --- a/packages/gitbook/src/lib/pages.ts +++ b/packages/gitbook/src/lib/pages.ts @@ -5,6 +5,7 @@ import { RevisionPageGroup, RevisionPageType, } from '@gitbook/api'; +import { headers } from 'next/headers'; export type AncestorRevisionPage = RevisionPageDocument | RevisionPageGroup; diff --git a/packages/gitbook/src/lib/pointer.ts b/packages/gitbook/src/lib/pointer.ts index 400fdd51d1..0e201db0a5 100644 --- a/packages/gitbook/src/lib/pointer.ts +++ b/packages/gitbook/src/lib/pointer.ts @@ -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 { 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; @@ -40,17 +47,17 @@ export async function getSiteContentPointer(): Promise { */ export async function getSpacePointer(): Promise { 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; diff --git a/packages/gitbook/src/middleware.ts b/packages/gitbook/src/middleware.ts index 7378145fca..71a3b56f4f 100644 --- a/packages/gitbook/src/middleware.ts +++ b/packages/gitbook/src/middleware.ts @@ -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(