From 7ab63516bd1cd480932c82e85ab40d43446fbaae Mon Sep 17 00:00:00 2001 From: Peter White <1788320+peterwhite@users.noreply.github.com> Date: Thu, 12 Mar 2026 11:06:33 +0100 Subject: [PATCH] Import site redirect patterns from @gitbook/api (#4107) --- .changeset/metal-pugs-bake.md | 5 +++++ packages/gitbook/src/components/SitePage/fetch.ts | 13 ++++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) create mode 100644 .changeset/metal-pugs-bake.md diff --git a/.changeset/metal-pugs-bake.md b/.changeset/metal-pugs-bake.md new file mode 100644 index 000000000..8533c4fe5 --- /dev/null +++ b/.changeset/metal-pugs-bake.md @@ -0,0 +1,5 @@ +--- +"gitbook": patch +--- + +Import site redirect patterns from @gitbook/api diff --git a/packages/gitbook/src/components/SitePage/fetch.ts b/packages/gitbook/src/components/SitePage/fetch.ts index 359ade537..b955d45ac 100644 --- a/packages/gitbook/src/components/SitePage/fetch.ts +++ b/packages/gitbook/src/components/SitePage/fetch.ts @@ -1,6 +1,11 @@ import type { GitBookSiteContext } from '@/lib/context'; import { redirect } from 'next/navigation'; +import { + SITE_REDIRECT_SOURCE_PATH_MAX_LENGTH, + SITE_REDIRECT_SOURCE_PATH_PATTERN, +} from '@gitbook/api'; + import { getDataOrNull } from '@/lib/data'; import { resolvePageId, resolvePagePath } from '@/lib/pages'; import { withLeadingSlash } from '@/lib/paths'; @@ -53,10 +58,8 @@ 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. // API has a limit of less than 512 characters for the source path, so we use the same limit here. - if (rawPathname.length < 512) { - // 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})+)*(?:\/\*|\*)?$/; + if (rawPathname.length < SITE_REDIRECT_SOURCE_PATH_MAX_LENGTH) { + const SITE_REDIRECT_SOURCE_PATH_REGEX = new RegExp(SITE_REDIRECT_SOURCE_PATH_PATTERN); const redirectPathname = withLeadingSlash(rawPathname); // If a page can't be found, we try with the API, in case we have a redirect at site level. const redirectSources = new Set( @@ -75,7 +78,7 @@ async function resolvePage(context: GitBookSiteContext, params: PagePathParams | for (const source of redirectSources) { // We try to resolve the site redirect const resolvedSiteRedirect = - source.length < 512 && + source.length < SITE_REDIRECT_SOURCE_PATH_MAX_LENGTH && (await getDataOrNull( context.dataFetcher.getSiteRedirectBySource({ organizationId,