From e2afc07ab2c815e68422b484fa491e2ae33d86b1 Mon Sep 17 00:00:00 2001 From: Taran Vohra Date: Wed, 2 Jul 2025 17:30:18 +0530 Subject: [PATCH] Fix resolution of page by resolving site redirects before space redirects (#3414) --- .changeset/green-bulldogs-punch.md | 5 +++ .../gitbook/src/components/SitePage/fetch.ts | 39 +++++++++---------- 2 files changed, 24 insertions(+), 20 deletions(-) create mode 100644 .changeset/green-bulldogs-punch.md diff --git a/.changeset/green-bulldogs-punch.md b/.changeset/green-bulldogs-punch.md new file mode 100644 index 000000000..d94960671 --- /dev/null +++ b/.changeset/green-bulldogs-punch.md @@ -0,0 +1,5 @@ +--- +"gitbook": patch +--- + +Fix resolution of page by resolving site redirects before space redirects diff --git a/packages/gitbook/src/components/SitePage/fetch.ts b/packages/gitbook/src/components/SitePage/fetch.ts index 7df552958..f27d7cebf 100644 --- a/packages/gitbook/src/components/SitePage/fetch.ts +++ b/packages/gitbook/src/components/SitePage/fetch.ts @@ -53,27 +53,12 @@ async function resolvePage(context: GitBookSiteContext, params: PagePathParams | // We don't test path that are too long as GitBook doesn't support them and will return a 404 anyway. if (rawPathname.length <= 512) { - // If page 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. - const resolved = await getDataOrNull( - context.dataFetcher.getRevisionPageByPath({ - spaceId: space.id, - revisionId: revisionId, - path: rawPathname, - }) - ); - if (resolved) { - return resolvePageId(revision.pages, resolved.id); - } - - // If a page still can't be found, we try with the API, in case we have a redirect at site level. + // Duplicated the regex pattern from SiteRedirectSourcePath API type. + const SITE_REDIRECT_SOURCE_PATH_REGEX = + /^\/(?:[A-Za-z0-9\-._~]|%[0-9A-Fa-f]{2})+(?:\/(?:[A-Za-z0-9\-._~]|%[0-9A-Fa-f]{2})+)*$/; const redirectPathname = withLeadingSlash(rawPathname); - if ( - /^\/(?:[A-Za-z0-9\-._~]|%[0-9A-Fa-f]{2})+(?:\/(?:[A-Za-z0-9\-._~]|%[0-9A-Fa-f]{2})+)*$/.test( - redirectPathname - ) - ) { + // If a page can't be found, we try with the API, in case we have a redirect at site level. + if (SITE_REDIRECT_SOURCE_PATH_REGEX.test(redirectPathname)) { const redirectSources = new Set([ // Test the pathname relative to the root // For example hello/world -> section/variant/hello/world @@ -98,6 +83,20 @@ async function resolvePage(context: GitBookSiteContext, params: PagePathParams | } } } + + // 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. + const resolved = await getDataOrNull( + context.dataFetcher.getRevisionPageByPath({ + spaceId: space.id, + revisionId: revisionId, + path: rawPathname, + }) + ); + if (resolved) { + return resolvePageId(revision.pages, resolved.id); + } } return undefined;