This commit is contained in:
Nicolas Dorseuil
2026-01-15 10:17:51 +01:00
parent 0470bdbefd
commit f3c1565aba
7 changed files with 60 additions and 45 deletions
@@ -28,13 +28,15 @@ function useVariantSpaceHref(variantSpace: VariantSpace, currentSpacePath: strin
}
const firstAlternate = metaLinks?.alternates[0];
const computed = firstAlternate ? {
pageID: firstAlternate.pageID,
spaceID: firstAlternate.space?.id
} : {
pageID: currentPage?.id,
spaceID: currentPage?.spaceId
};
const computed = firstAlternate
? {
pageID: firstAlternate.pageID,
spaceID: firstAlternate.space?.id,
}
: {
pageID: currentPage?.id,
spaceID: currentPage?.spaceId,
};
// If there is no alternate link, we reconstruct the URL by swapping the space path.
@@ -53,7 +55,7 @@ function useVariantSpaceHref(variantSpace: VariantSpace, currentSpacePath: strin
targetUrl.searchParams.set('fallback', 'true');
if(computed?.spaceID && computed?.pageID) {
if (computed?.spaceID && computed?.pageID) {
targetUrl.searchParams.set('fallbackPageID', computed.pageID);
targetUrl.searchParams.set('fallbackSpaceID', computed.spaceID);
}
@@ -61,7 +63,7 @@ function useVariantSpaceHref(variantSpace: VariantSpace, currentSpacePath: strin
return targetUrl.toString();
}
if(computed?.spaceID && computed?.pageID) {
if (computed?.spaceID && computed?.pageID) {
return `${joinPath(variantSpaceUrl, currentPathname)}?fallback=true&fallbackPageID=${computed.pageID}&fallbackSpaceID=${computed.spaceID}`;
}
@@ -11,7 +11,7 @@ import type { PageMetaLinks } from './SitePage';
*/
export function PageClientLayout({
pageMetaLinks,
currentPage
currentPage,
}: {
pageMetaLinks: PageMetaLinks | null;
currentPage: {
@@ -106,7 +106,10 @@ export async function SitePage(props: SitePageProps & { staticRoute: boolean })
staticRoute={props.staticRoute}
/>
</div>
<PageClientLayout pageMetaLinks={pageMetaLinks} currentPage={{ id: page.id, spaceId: context.space.id }} />
<PageClientLayout
pageMetaLinks={pageMetaLinks}
currentPage={{ id: page.id, spaceId: context.space.id }}
/>
</div>
</PageContextProvider>
);
@@ -354,7 +357,13 @@ async function resolvePageMetaLinks(
return {
canonical: resolvedCanonical ?? null,
alternates: resolvedAlternates.filter(
(alt): alt is { href: string; space: AlternateLinkSpace | null, pageID: string | undefined } => !!alt.href
(
alt
): alt is {
href: string;
space: AlternateLinkSpace | null;
pageID: string | undefined;
} => !!alt.href
),
};
}
@@ -36,7 +36,8 @@ export async function fetchPageData(context: GitBookSiteContext, params: PagePar
* If the path can't be found, we try to resolve it from the API to handle redirects.
*/
async function resolvePage(context: GitBookSiteContext, params: PagePathParams | PageIdParams) {
const { organizationId, site, space, revision, shareKey, linker, revisionId, fallback } = context;
const { organizationId, site, space, revision, shareKey, linker, revisionId, fallback } =
context;
if ('pageId' in params) {
return resolvePageId(revision.pages, params.pageId);
@@ -100,12 +101,9 @@ async function resolvePage(context: GitBookSiteContext, params: PagePathParams |
);
if (resolved) {
return resolvePageId(revision.pages, resolved.id);
} else if(fallback.isFallback && fallback.pageID && fallback.spaceID) {
return resolveFallbackPage(
revision.pages,
fallback.spaceID,
fallback.pageID
);
// biome-ignore lint/style/noUselessElse: Not useless
} else if (fallback.isFallback && fallback.pageID && fallback.spaceID) {
return resolveFallbackPage(revision.pages, fallback.spaceID, fallback.pageID);
}
}
+13 -13
View File
@@ -29,21 +29,21 @@ import { type GitBookLinker, createLinker, linkerForPublishedURL } from './links
type FallbackData = {
/**
*
*/
isFallback: boolean;
*
*/
isFallback: boolean;
/**
* Space ID of the main space.
* Only provided for a computed space.
*/
spaceID?: string;
/**
* Space ID of the main space.
* Only provided for a computed space.
*/
spaceID?: string;
/**
* page ID of the main revision.
* Only provided for a computed revision.
*/
pageID?: string;
/**
* page ID of the main revision.
* Only provided for a computed revision.
*/
pageID?: string;
};
/**
+16 -9
View File
@@ -117,16 +117,16 @@ export function resolvePageId(
}
/**
*
*
* Resolve a page by its related space ID and page ID from the revision pages.
* Related pages are pages that are linked via meta links alternates.
* It uses the metaLinks alternates to find the matching computed page.
* It always use the first alternate to resolve the parent page.
*
* @param rootPages
*
* @param rootPages
* @param relatedSpaceId Space ID of the parent page (i.e. )
* @param relatedPageId
* @returns
* @param relatedPageId
* @returns
*/
export function resolveFallbackPage(
rootPages: Revision['pages'],
@@ -142,10 +142,17 @@ export function resolveFallbackPage(
continue;
}
if(page.type === RevisionPageType.Document) {
if (page.type === RevisionPageType.Document) {
const metaLinksAlternates = page.metaLinks?.alternates;
if(metaLinksAlternates && metaLinksAlternates.length > 0 && metaLinksAlternates[0]?.kind !== "url") {
if(metaLinksAlternates[0]?.space === relatedSpaceId && metaLinksAlternates[0]?.page === relatedPageId) {
if (
metaLinksAlternates &&
metaLinksAlternates.length > 0 &&
metaLinksAlternates[0]?.kind !== 'url'
) {
if (
metaLinksAlternates[0]?.space === relatedSpaceId &&
metaLinksAlternates[0]?.page === relatedPageId
) {
return resolvePageDocument(page, ancestors);
}
}
@@ -264,7 +271,7 @@ function resolvePageDocument(
return;
}
if (page.type === RevisionPageType.Link || page.type === RevisionPageType.Computed) {
return undefined;
return undefined;
}
return { page, ancestors };
+3 -4
View File
@@ -285,7 +285,7 @@ async function serveSiteRoutes(requestURL: URL, request: NextRequest) {
// Extract fallback pageID and spaceID from the URL
// These are used when switching variant spaces to redirect to a specific page if the current path doesn't exist in the new variant.
// Because there is no link between every variant, we need to pass a stable spaceID/pageID to redirect to.
// They are the first alternate link in the page metadata of the previous variant.
// They are the first alternate link in the page metadata of the previous variant.
const fallbackPageID = requestURL.searchParams.get('fallbackPageID');
const fallbackSpaceID = requestURL.searchParams.get('fallbackSpaceID');
@@ -306,11 +306,10 @@ async function serveSiteRoutes(requestURL: URL, request: NextRequest) {
imagesContextId: imagesContextId,
contextId: siteURLData.contextId,
fallback: {
isFallback: requestURL.searchParams.get('fallback') === 'true' ? true : false,
isFallback: requestURL.searchParams.get('fallback') === 'true',
...(fallbackPageID ? { pageID: fallbackPageID } : {}),
...(fallbackSpaceID ? { spaceID: fallbackSpaceID } : {}),
}
},
};
const requestHeaders = new Headers(request.headers);