Import site redirect patterns from @gitbook/api (#4107)

This commit is contained in:
Peter White
2026-03-12 11:06:33 +01:00
committed by GitHub
parent 9dd2db6678
commit 7ab63516bd
2 changed files with 13 additions and 5 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"gitbook": patch
---
Import site redirect patterns from @gitbook/api
@@ -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,