From 32305f4fcb1f82053f7b9ffb21568a8f9295dac7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Berg=C3=A9?= Date: Wed, 15 Jan 2025 10:23:47 +0100 Subject: [PATCH 1/3] Fix error when accessing some not found pages Fix GITBOOK-OPEN-1XDB Fix GITBOOK-OPEN-1XDC Fix GITBOOK-OPEN-1X07 Fix GITBOOK-OPEN-1XCJ Fix GITBOOK-OPEN-1X86 --- .changeset/thirty-berries-bow.md | 5 ++ .../(content)/[[...pathname]]/not-found.tsx | 19 +++++++- .../(site)/(content)/[[...pathname]]/page.tsx | 20 +++++++- .../src/app/(site)/(content)/layout.tsx | 18 +++++++ packages/gitbook/src/app/(site)/layout.tsx | 16 ++++++- .../src/components/DocumentView/Embed.tsx | 4 +- packages/gitbook/src/lib/csp.ts | 7 ++- packages/gitbook/src/lib/pages.ts | 11 +++++ packages/gitbook/src/lib/pointer.ts | 47 +++++++++++-------- 9 files changed, 117 insertions(+), 30 deletions(-) create mode 100644 .changeset/thirty-berries-bow.md 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)/[[...pathname]]/not-found.tsx b/packages/gitbook/src/app/(site)/(content)/[[...pathname]]/not-found.tsx index cd36e5b268..24e2d7bc3f 100644 --- a/packages/gitbook/src/app/(site)/(content)/[[...pathname]]/not-found.tsx +++ b/packages/gitbook/src/app/(site)/(content)/[[...pathname]]/not-found.tsx @@ -1,16 +1,33 @@ +import Link from 'next/link'; + import { TrackPageViewEvent } from '@/components/Insights'; import { getSpaceLanguage, t } from '@/intl/server'; +import { languages } from '@/intl/translations'; import { getSiteData, getSpaceContentData } from '@/lib/api'; +import { checkIsFromMiddleware } from '@/lib/pages'; import { getSiteContentPointer } from '@/lib/pointer'; import { tcls } from '@/lib/tailwind'; export default async function NotFound() { + const fromMiddleware = await checkIsFromMiddleware(); + if (!fromMiddleware) { + return ( +
+
+

Not found

+

This page could not be found

+ + Go back to home + +
+
+ ); + } const pointer = await getSiteContentPointer(); const [{ space }, { customization }] = await Promise.all([ getSpaceContentData(pointer, pointer.siteShareKey), getSiteData(pointer), ]); - const language = getSpaceLanguage(customization); return ( diff --git a/packages/gitbook/src/app/(site)/(content)/[[...pathname]]/page.tsx b/packages/gitbook/src/app/(site)/(content)/[[...pathname]]/page.tsx index 786d5d6eb8..3bcae9c4b1 100644 --- a/packages/gitbook/src/app/(site)/(content)/[[...pathname]]/page.tsx +++ b/packages/gitbook/src/app/(site)/(content)/[[...pathname]]/page.tsx @@ -1,12 +1,13 @@ import { CustomizationHeaderPreset, CustomizationThemeMode } from '@gitbook/api'; import { Metadata, Viewport } from 'next'; +import { headers } from 'next/headers'; import { notFound, redirect } from 'next/navigation'; import React from 'react'; import { PageAside } from '@/components/PageAside'; import { PageBody, PageCover } from '@/components/PageBody'; import { PageHrefContext, getAbsoluteHref, getPageHref } from '@/lib/links'; -import { getPagePath, resolveFirstDocument } from '@/lib/pages'; +import { checkIsFromMiddleware, getPagePath, resolveFirstDocument } from '@/lib/pages'; import { ContentRefContext } from '@/lib/references'; import { isSpaceIndexable, isPageIndexable } from '@/lib/seo'; import { getContentTitle } from '@/lib/utils'; @@ -24,6 +25,8 @@ export default async function Page(props: { params: Promise; searchParams: Promise<{ fallback?: string }>; }) { + await ensureIsFromMiddleware(); + const { params: rawParams, searchParams: rawSearchParams } = props; const params = await rawParams; @@ -127,6 +130,7 @@ export async function generateViewport({ }: { params: Promise; }): Promise { + await ensureIsFromMiddleware(); const { customization } = await fetchPageData(await params); return { colorScheme: customization.themes.toggeable @@ -144,6 +148,8 @@ export async function generateMetadata({ params: Promise; searchParams: Promise<{ fallback?: string }>; }): Promise { + await ensureIsFromMiddleware(); + const { space, pages, page, customization, site, ancestors } = await getPageDataWithFallback({ pagePathParams: await params, searchParams: await searchParams, @@ -206,3 +212,15 @@ async function getPageDataWithFallback(args: { page, }; } + +/** + * Returns a page not found if the request is not from the middleware. + * Some pages can be + */ +async function ensureIsFromMiddleware() { + // To check if the request is from the middleware, we check if the x-gitbook-token is set in the headers. + const fromMiddleware = await checkIsFromMiddleware(); + if (!fromMiddleware) { + notFound(); + } +} diff --git a/packages/gitbook/src/app/(site)/(content)/layout.tsx b/packages/gitbook/src/app/(site)/(content)/layout.tsx index 9c0d17476a..06ac17b93a 100644 --- a/packages/gitbook/src/app/(site)/(content)/layout.tsx +++ b/packages/gitbook/src/app/(site)/(content)/layout.tsx @@ -14,6 +14,7 @@ import { assetsDomain } from '@/lib/assets'; import { buildVersion } from '@/lib/build'; import { getContentSecurityPolicyNonce } from '@/lib/csp'; import { getAbsoluteHref, getBaseUrl } from '@/lib/links'; +import { checkIsFromMiddleware } from '@/lib/pages'; import { isSpaceIndexable } from '@/lib/seo'; import { getContentTitle } from '@/lib/utils'; @@ -29,6 +30,10 @@ export const dynamic = 'force-dynamic'; */ export default async function ContentLayout(props: { children: React.ReactNode }) { const { children } = props; + const fromMiddleware = await checkIsFromMiddleware(); + if (!fromMiddleware) { + return props.children; + } const nonce = await getContentSecurityPolicyNonce(); const { @@ -106,6 +111,11 @@ export default async function ContentLayout(props: { children: React.ReactNode } } export async function generateViewport(): Promise { + const fromMiddleware = await checkIsFromMiddleware(); + if (!fromMiddleware) { + return {}; + } + const { customization } = await fetchContentData(); return { colorScheme: customization.themes.toggeable @@ -117,6 +127,14 @@ export async function generateViewport(): Promise { } export async function generateMetadata(): Promise { + const fromMiddleware = await checkIsFromMiddleware(); + if (!fromMiddleware) { + return { + title: 'Not found', + robots: 'noindex, nofollow', + }; + } + const { space, site, customization } = await fetchContentData(); const customIcon = 'icon' in customization.favicon ? customization.favicon.icon : null; diff --git a/packages/gitbook/src/app/(site)/layout.tsx b/packages/gitbook/src/app/(site)/layout.tsx index 431c97b7d6..d3401ee8a2 100644 --- a/packages/gitbook/src/app/(site)/layout.tsx +++ b/packages/gitbook/src/app/(site)/layout.tsx @@ -1,5 +1,6 @@ import { CustomizationRootLayout } from '@/components/RootLayout'; import { getSiteData } from '@/lib/api'; +import { checkIsFromMiddleware } from '@/lib/pages'; import { getSiteContentPointer } from '@/lib/pointer'; /** @@ -7,12 +8,23 @@ import { getSiteContentPointer } from '@/lib/pointer'; * and initializes the CustomizationRootLayout with it. */ export default async function SiteRootLayout(props: { children: React.ReactNode }) { - const { children } = props; + const fromMiddleware = await checkIsFromMiddleware(); + if (!fromMiddleware) { + return ( + + +
{props.children}
+ + + ); + } const pointer = await getSiteContentPointer(); const { customization } = await getSiteData(pointer); return ( - {children} + + {props.children} + ); } 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..29e94bba50 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; @@ -184,3 +185,13 @@ function flattenPages( return result; } + +/** + * Returns a page not found if the request is not from the middleware. + * Some pages can be + */ +export async function checkIsFromMiddleware() { + const headerList = await headers(); + // To check if the request is from the middleware, we check if the x-gitbook-token is set in the headers. + return Boolean(headerList.get('x-gitbook-token')); +} 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; From 3e2bbf28b2385b017c9de8405612b1bde83864f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Berg=C3=A9?= Date: Wed, 15 Jan 2025 11:18:27 +0100 Subject: [PATCH 2/3] Scope middleware into a sub folder --- .../(site)/(content)/ClientContexts.tsx | 0 .../(site)/(content)/RocketLoaderDetector.tsx | 0 .../[[...pathname]]/PageClientLayout.tsx | 0 .../(content)/[[...pathname]]/loading.tsx | 0 .../(content)/[[...pathname]]/not-found.tsx | 19 +----------------- .../(site)/(content)/[[...pathname]]/page.tsx | 20 +------------------ .../(site)/(content)/layout.tsx | 18 ----------------- .../(site)/(core)/robots.txt/route.ts | 0 .../(site)/(core)/sitemap.xml/route.ts | 0 .../(site)/(core)/~gitbook/icon/route.tsx | 0 .../~gitbook/ogimage/[pageId]/route.tsx | 0 .../src/app/{ => middleware}/(site)/error.tsx | 0 .../src/app/{ => middleware}/(site)/fetch.ts | 0 .../app/{ => middleware}/(site)/layout.tsx | 16 ++------------- .../~gitbook/pdf/PageControlButtons.tsx | 0 .../(space)/~gitbook/pdf/PrintButton.tsx | 0 .../(space)/~gitbook/pdf/layout.tsx | 0 .../(space)/~gitbook/pdf/page.tsx | 0 .../(space)/~gitbook/pdf/pdf.css | 0 .../(space)/~gitbook/pdf/pointer.ts | 0 packages/gitbook/src/middleware.ts | 3 +-- 21 files changed, 5 insertions(+), 71 deletions(-) rename packages/gitbook/src/app/{ => middleware}/(site)/(content)/ClientContexts.tsx (100%) rename packages/gitbook/src/app/{ => middleware}/(site)/(content)/RocketLoaderDetector.tsx (100%) rename packages/gitbook/src/app/{ => middleware}/(site)/(content)/[[...pathname]]/PageClientLayout.tsx (100%) rename packages/gitbook/src/app/{ => middleware}/(site)/(content)/[[...pathname]]/loading.tsx (100%) rename packages/gitbook/src/app/{ => middleware}/(site)/(content)/[[...pathname]]/not-found.tsx (61%) rename packages/gitbook/src/app/{ => middleware}/(site)/(content)/[[...pathname]]/page.tsx (92%) rename packages/gitbook/src/app/{ => middleware}/(site)/(content)/layout.tsx (91%) rename packages/gitbook/src/app/{ => middleware}/(site)/(core)/robots.txt/route.ts (100%) rename packages/gitbook/src/app/{ => middleware}/(site)/(core)/sitemap.xml/route.ts (100%) rename packages/gitbook/src/app/{ => middleware}/(site)/(core)/~gitbook/icon/route.tsx (100%) rename packages/gitbook/src/app/{ => middleware}/(site)/(core)/~gitbook/ogimage/[pageId]/route.tsx (100%) rename packages/gitbook/src/app/{ => middleware}/(site)/error.tsx (100%) rename packages/gitbook/src/app/{ => middleware}/(site)/fetch.ts (100%) rename packages/gitbook/src/app/{ => middleware}/(site)/layout.tsx (52%) rename packages/gitbook/src/app/{ => middleware}/(space)/~gitbook/pdf/PageControlButtons.tsx (100%) rename packages/gitbook/src/app/{ => middleware}/(space)/~gitbook/pdf/PrintButton.tsx (100%) rename packages/gitbook/src/app/{ => middleware}/(space)/~gitbook/pdf/layout.tsx (100%) rename packages/gitbook/src/app/{ => middleware}/(space)/~gitbook/pdf/page.tsx (100%) rename packages/gitbook/src/app/{ => middleware}/(space)/~gitbook/pdf/pdf.css (100%) rename packages/gitbook/src/app/{ => middleware}/(space)/~gitbook/pdf/pointer.ts (100%) 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 61% 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 index 24e2d7bc3f..cd36e5b268 100644 --- a/packages/gitbook/src/app/(site)/(content)/[[...pathname]]/not-found.tsx +++ b/packages/gitbook/src/app/middleware/(site)/(content)/[[...pathname]]/not-found.tsx @@ -1,33 +1,16 @@ -import Link from 'next/link'; - import { TrackPageViewEvent } from '@/components/Insights'; import { getSpaceLanguage, t } from '@/intl/server'; -import { languages } from '@/intl/translations'; import { getSiteData, getSpaceContentData } from '@/lib/api'; -import { checkIsFromMiddleware } from '@/lib/pages'; import { getSiteContentPointer } from '@/lib/pointer'; import { tcls } from '@/lib/tailwind'; export default async function NotFound() { - const fromMiddleware = await checkIsFromMiddleware(); - if (!fromMiddleware) { - return ( -
-
-

Not found

-

This page could not be found

- - Go back to home - -
-
- ); - } const pointer = await getSiteContentPointer(); const [{ space }, { customization }] = await Promise.all([ getSpaceContentData(pointer, pointer.siteShareKey), getSiteData(pointer), ]); + const language = getSpaceLanguage(customization); return ( 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 92% rename from packages/gitbook/src/app/(site)/(content)/[[...pathname]]/page.tsx rename to packages/gitbook/src/app/middleware/(site)/(content)/[[...pathname]]/page.tsx index 3bcae9c4b1..786d5d6eb8 100644 --- a/packages/gitbook/src/app/(site)/(content)/[[...pathname]]/page.tsx +++ b/packages/gitbook/src/app/middleware/(site)/(content)/[[...pathname]]/page.tsx @@ -1,13 +1,12 @@ import { CustomizationHeaderPreset, CustomizationThemeMode } from '@gitbook/api'; import { Metadata, Viewport } from 'next'; -import { headers } from 'next/headers'; import { notFound, redirect } from 'next/navigation'; import React from 'react'; import { PageAside } from '@/components/PageAside'; import { PageBody, PageCover } from '@/components/PageBody'; import { PageHrefContext, getAbsoluteHref, getPageHref } from '@/lib/links'; -import { checkIsFromMiddleware, getPagePath, resolveFirstDocument } from '@/lib/pages'; +import { getPagePath, resolveFirstDocument } from '@/lib/pages'; import { ContentRefContext } from '@/lib/references'; import { isSpaceIndexable, isPageIndexable } from '@/lib/seo'; import { getContentTitle } from '@/lib/utils'; @@ -25,8 +24,6 @@ export default async function Page(props: { params: Promise; searchParams: Promise<{ fallback?: string }>; }) { - await ensureIsFromMiddleware(); - const { params: rawParams, searchParams: rawSearchParams } = props; const params = await rawParams; @@ -130,7 +127,6 @@ export async function generateViewport({ }: { params: Promise; }): Promise { - await ensureIsFromMiddleware(); const { customization } = await fetchPageData(await params); return { colorScheme: customization.themes.toggeable @@ -148,8 +144,6 @@ export async function generateMetadata({ params: Promise; searchParams: Promise<{ fallback?: string }>; }): Promise { - await ensureIsFromMiddleware(); - const { space, pages, page, customization, site, ancestors } = await getPageDataWithFallback({ pagePathParams: await params, searchParams: await searchParams, @@ -212,15 +206,3 @@ async function getPageDataWithFallback(args: { page, }; } - -/** - * Returns a page not found if the request is not from the middleware. - * Some pages can be - */ -async function ensureIsFromMiddleware() { - // To check if the request is from the middleware, we check if the x-gitbook-token is set in the headers. - const fromMiddleware = await checkIsFromMiddleware(); - if (!fromMiddleware) { - notFound(); - } -} diff --git a/packages/gitbook/src/app/(site)/(content)/layout.tsx b/packages/gitbook/src/app/middleware/(site)/(content)/layout.tsx similarity index 91% rename from packages/gitbook/src/app/(site)/(content)/layout.tsx rename to packages/gitbook/src/app/middleware/(site)/(content)/layout.tsx index 06ac17b93a..9c0d17476a 100644 --- a/packages/gitbook/src/app/(site)/(content)/layout.tsx +++ b/packages/gitbook/src/app/middleware/(site)/(content)/layout.tsx @@ -14,7 +14,6 @@ import { assetsDomain } from '@/lib/assets'; import { buildVersion } from '@/lib/build'; import { getContentSecurityPolicyNonce } from '@/lib/csp'; import { getAbsoluteHref, getBaseUrl } from '@/lib/links'; -import { checkIsFromMiddleware } from '@/lib/pages'; import { isSpaceIndexable } from '@/lib/seo'; import { getContentTitle } from '@/lib/utils'; @@ -30,10 +29,6 @@ export const dynamic = 'force-dynamic'; */ export default async function ContentLayout(props: { children: React.ReactNode }) { const { children } = props; - const fromMiddleware = await checkIsFromMiddleware(); - if (!fromMiddleware) { - return props.children; - } const nonce = await getContentSecurityPolicyNonce(); const { @@ -111,11 +106,6 @@ export default async function ContentLayout(props: { children: React.ReactNode } } export async function generateViewport(): Promise { - const fromMiddleware = await checkIsFromMiddleware(); - if (!fromMiddleware) { - return {}; - } - const { customization } = await fetchContentData(); return { colorScheme: customization.themes.toggeable @@ -127,14 +117,6 @@ export async function generateViewport(): Promise { } export async function generateMetadata(): Promise { - const fromMiddleware = await checkIsFromMiddleware(); - if (!fromMiddleware) { - return { - title: 'Not found', - robots: 'noindex, nofollow', - }; - } - const { space, site, customization } = await fetchContentData(); const customIcon = 'icon' in customization.favicon ? customization.favicon.icon : null; 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 52% rename from packages/gitbook/src/app/(site)/layout.tsx rename to packages/gitbook/src/app/middleware/(site)/layout.tsx index d3401ee8a2..431c97b7d6 100644 --- a/packages/gitbook/src/app/(site)/layout.tsx +++ b/packages/gitbook/src/app/middleware/(site)/layout.tsx @@ -1,6 +1,5 @@ import { CustomizationRootLayout } from '@/components/RootLayout'; import { getSiteData } from '@/lib/api'; -import { checkIsFromMiddleware } from '@/lib/pages'; import { getSiteContentPointer } from '@/lib/pointer'; /** @@ -8,23 +7,12 @@ import { getSiteContentPointer } from '@/lib/pointer'; * and initializes the CustomizationRootLayout with it. */ export default async function SiteRootLayout(props: { children: React.ReactNode }) { - const fromMiddleware = await checkIsFromMiddleware(); - if (!fromMiddleware) { - return ( - - -
{props.children}
- - - ); - } + const { children } = props; const pointer = await getSiteContentPointer(); const { customization } = await getSiteData(pointer); return ( - - {props.children} - + {children} ); } 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/middleware.ts b/packages/gitbook/src/middleware.ts index 7378145fca..52925a1157 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('/middleware' + rewritePathname, request.nextUrl.toString()); target.search = url.search; const response = writeCookies( From b20bcaaf7aa93701e3a2efba1bbffd539bf57038 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Berg=C3=A9?= Date: Wed, 15 Jan 2025 11:47:27 +0100 Subject: [PATCH 3/3] Use join path, remove unused method --- packages/gitbook/src/lib/pages.ts | 10 ---------- packages/gitbook/src/middleware.ts | 2 +- 2 files changed, 1 insertion(+), 11 deletions(-) diff --git a/packages/gitbook/src/lib/pages.ts b/packages/gitbook/src/lib/pages.ts index 29e94bba50..991c40fa36 100644 --- a/packages/gitbook/src/lib/pages.ts +++ b/packages/gitbook/src/lib/pages.ts @@ -185,13 +185,3 @@ function flattenPages( return result; } - -/** - * Returns a page not found if the request is not from the middleware. - * Some pages can be - */ -export async function checkIsFromMiddleware() { - const headerList = await headers(); - // To check if the request is from the middleware, we check if the x-gitbook-token is set in the headers. - return Boolean(headerList.get('x-gitbook-token')); -} diff --git a/packages/gitbook/src/middleware.ts b/packages/gitbook/src/middleware.ts index 52925a1157..71a3b56f4f 100644 --- a/packages/gitbook/src/middleware.ts +++ b/packages/gitbook/src/middleware.ts @@ -277,7 +277,7 @@ export async function middleware(request: NextRequest) { headers.set('x-gitbook-visitor-token', resolved.visitorToken); } - const target = new URL('/middleware' + rewritePathname, request.nextUrl.toString()); + const target = new URL(joinPath('/middleware', rewritePathname), request.nextUrl.toString()); target.search = url.search; const response = writeCookies(