From deea1b718aa8d22669810612145c034f74cd155f Mon Sep 17 00:00:00 2001 From: Claire Chabas Date: Wed, 25 Nov 2026 18:02:11 +0000 Subject: [PATCH] Check space redirects before page ID URLs Co-Authored-By: Claude Opus 5.5 --- .../src/components/SitePage/fetch.test.ts | 11 ++++++++ .../gitbook/src/components/SitePage/fetch.ts | 26 +++++++++++-------- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/packages/gitbook/src/components/SitePage/fetch.test.ts b/packages/gitbook/src/components/SitePage/fetch.test.ts index 6307e6b11..14586c99d 100644 --- a/packages/gitbook/src/components/SitePage/fetch.test.ts +++ b/packages/gitbook/src/components/SitePage/fetch.test.ts @@ -354,6 +354,17 @@ describe('resolveMissingPagePath', () => { }); }); + it('keeps a space redirect from a page ID path over the page with that ID', async () => { + const context = createRedirectContext({ spaceRedirectPageId: page.id }); + const idPage = { ...page, id: 'wwCLFQyPl9cctUGxdLZm' }; + context.revision.pages = [page, idPage]; + + expect(await resolveMissingPagePath(context, 'pages/wwCLFQyPl9cctUGxdLZm')).toEqual({ + type: 'page', + page: { page, ancestors: [] }, + }); + }); + it('returns undefined when nothing matches', async () => { const context = createRedirectContext({}); diff --git a/packages/gitbook/src/components/SitePage/fetch.ts b/packages/gitbook/src/components/SitePage/fetch.ts index fb8e9e152..19f3b04e9 100644 --- a/packages/gitbook/src/components/SitePage/fetch.ts +++ b/packages/gitbook/src/components/SitePage/fetch.ts @@ -171,15 +171,6 @@ export async function resolveMissingPagePath( } } - // Content-ref page links (`/pages/`) can reach visitors; IDs are case-sensitive, so match the raw pathname. - const contentRef = resolveStringContentRef(redirectPathname); - if (contentRef?.kind === 'page' && !contentRef.space) { - const page = resolvePageId(revision.pages, contentRef.page); - if (page) { - return { type: 'page', page }; - } - } - // If page still can't be found, we try with the API, in case we have a redirect at space level. // We use the raw pathname to handle special/malformed redirects setup by users in the GitSync. // The page rendering will take care of redirecting to a normalized pathname. @@ -190,8 +181,21 @@ export async function resolveMissingPagePath( path: rawPathname, }) ); - const page = resolved ? resolvePageId(revision.pages, resolved.id) : undefined; - return page ? { type: 'page', page } : undefined; + const redirectedPage = resolved ? resolvePageId(revision.pages, resolved.id) : undefined; + if (redirectedPage) { + return { type: 'page', page: redirectedPage }; + } + + // Content-ref page links (`/pages/`) can reach visitors; IDs are case-sensitive, so match the raw pathname. + const contentRef = resolveStringContentRef(redirectPathname); + if (contentRef?.kind === 'page' && !contentRef.space) { + const page = resolvePageId(revision.pages, contentRef.page); + if (page) { + return { type: 'page', page }; + } + } + + return undefined; } /**