From 79ce105caa84fc35ff13bcdc59676ebc0d4640da Mon Sep 17 00:00:00 2001 From: Zeno Kapitein Date: Tue, 18 Nov 2025 19:38:20 +0100 Subject: [PATCH] Fix 404 for embed root page Going to `/~gitbook/embed/page/` should load the space's root page, but returns a 404 instead. This fixes that. --- .../page/{[pagePath] => [[...pagePath]]}/page.tsx | 0 .../page/{[pagePath] => [[...pagePath]]}/page.tsx | 0 packages/gitbook/src/app/utils.ts | 8 +++++--- packages/gitbook/src/middleware.ts | 10 ++++------ 4 files changed, 9 insertions(+), 9 deletions(-) rename packages/gitbook/src/app/sites/dynamic/[mode]/[siteURL]/[siteData]/~gitbook/embed/page/{[pagePath] => [[...pagePath]]}/page.tsx (100%) rename packages/gitbook/src/app/sites/static/[mode]/[siteURL]/[siteData]/~gitbook/embed/page/{[pagePath] => [[...pagePath]]}/page.tsx (100%) diff --git a/packages/gitbook/src/app/sites/dynamic/[mode]/[siteURL]/[siteData]/~gitbook/embed/page/[pagePath]/page.tsx b/packages/gitbook/src/app/sites/dynamic/[mode]/[siteURL]/[siteData]/~gitbook/embed/page/[[...pagePath]]/page.tsx similarity index 100% rename from packages/gitbook/src/app/sites/dynamic/[mode]/[siteURL]/[siteData]/~gitbook/embed/page/[pagePath]/page.tsx rename to packages/gitbook/src/app/sites/dynamic/[mode]/[siteURL]/[siteData]/~gitbook/embed/page/[[...pagePath]]/page.tsx diff --git a/packages/gitbook/src/app/sites/static/[mode]/[siteURL]/[siteData]/~gitbook/embed/page/[pagePath]/page.tsx b/packages/gitbook/src/app/sites/static/[mode]/[siteURL]/[siteData]/~gitbook/embed/page/[[...pagePath]]/page.tsx similarity index 100% rename from packages/gitbook/src/app/sites/static/[mode]/[siteURL]/[siteData]/~gitbook/embed/page/[pagePath]/page.tsx rename to packages/gitbook/src/app/sites/static/[mode]/[siteURL]/[siteData]/~gitbook/embed/page/[[...pagePath]]/page.tsx diff --git a/packages/gitbook/src/app/utils.ts b/packages/gitbook/src/app/utils.ts index 0e5d1688b..51cb30d29 100644 --- a/packages/gitbook/src/app/utils.ts +++ b/packages/gitbook/src/app/utils.ts @@ -19,7 +19,7 @@ export type RouteLayoutParams = { }; export type RouteParams = RouteLayoutParams & { - pagePath: string; + pagePath?: string[]; }; /** @@ -80,8 +80,10 @@ export async function getDynamicSiteContext(params: RouteLayoutParams) { * Get the decoded page path from the params. */ export function getPagePathFromParams(params: RouteParams) { - const decoded = decodeURIComponent(params.pagePath); - return decoded; + if (!params.pagePath || params.pagePath.length === 0) { + return ''; + } + return params.pagePath.map((part) => decodeURIComponent(part)).join('/'); } function getSiteURLFromParams(params: RouteLayoutParams) { diff --git a/packages/gitbook/src/middleware.ts b/packages/gitbook/src/middleware.ts index 915f2cc70..1a64bdbb1 100644 --- a/packages/gitbook/src/middleware.ts +++ b/packages/gitbook/src/middleware.ts @@ -589,12 +589,10 @@ function encodePathInSiteContent(rawPathname: string): { return { pathname, routeType: 'static' }; } - // If the pathname is an embedded page - const embedPage = pathname.match(/^~gitbook\/embed\/page\/(\S+)$/); - if (embedPage) { - return { - pathname: `~gitbook/embed/page/${encodeURIComponent(embedPage[1]!)}`, - }; + // If the pathname is an embedded page, let the optional catch-all route [[...pagePath]] handle it + // This naturally supports both empty paths (~gitbook/embed/page) and paths with segments + if (pathname.match(/^~gitbook\/embed\/page(\/.*)?$/)) { + return { pathname }; } switch (pathname) {