diff --git a/packages/gitbook/src/components/SitePage/SitePage.tsx b/packages/gitbook/src/components/SitePage/SitePage.tsx index 495493e814..5e81d29111 100644 --- a/packages/gitbook/src/components/SitePage/SitePage.tsx +++ b/packages/gitbook/src/components/SitePage/SitePage.tsx @@ -317,13 +317,34 @@ async function resolvePageMetaLinks( context: GitBookSiteContext, pageId: string ): Promise { - const pageMetaLinks = await getDataOrNull( - context.dataFetcher.listRevisionPageMetaLinks({ - spaceId: context.space.id, - revisionId: context.revisionId, - pageId, - }) - ); + const pageMetaLinks = await (async () => { + if (context.changeRequest) { + return getDataOrNull( + context.dataFetcher.listChangeRequestPageMetaLinks({ + spaceId: context.space.id, + changeRequestId: context.changeRequest.id, + pageId, + }) + ); + } + + if (context.revisionId !== context.space.revision) { + return getDataOrNull( + context.dataFetcher.listRevisionPageMetaLinks({ + spaceId: context.space.id, + revisionId: context.revisionId, + pageId, + }) + ); + } + + return getDataOrNull( + context.dataFetcher.listSpacePageMetaLinks({ + spaceId: context.space.id, + pageId, + }) + ); + })(); if (pageMetaLinks) { const canonicalResolution = pageMetaLinks.canonical diff --git a/packages/gitbook/src/lib/data/api.ts b/packages/gitbook/src/lib/data/api.ts index b66af936ce..366db3be4d 100644 --- a/packages/gitbook/src/lib/data/api.ts +++ b/packages/gitbook/src/lib/data/api.ts @@ -159,6 +159,21 @@ export function createDataFetcher( pageId: params.pageId, }); }, + + listSpacePageMetaLinks(params) { + return listSpacePageMetaLinks(input, { + spaceId: params.spaceId, + pageId: params.pageId, + }); + }, + + listChangeRequestPageMetaLinks(params) { + return listChangeRequestPageMetaLinks(input, { + spaceId: params.spaceId, + changeRequestId: params.changeRequestId, + pageId: params.pageId, + }); + }, }; } @@ -744,6 +759,64 @@ const listRevisionPageMetaLinks = cache( } ); +/** + * List all the meta links for a given page in a space. + */ +const listSpacePageMetaLinks = cache( + async (input: DataFetcherInput, params: { spaceId: string; pageId: string }) => { + 'use cache'; + return wrapCacheDataFetcherError(async () => { + return trace( + `listSpacePageMetaLinks(${params.spaceId}, ${params.pageId})`, + async () => { + const api = apiClient(input); + const res = await api.spaces.listSpacePageMetaLinks( + params.spaceId, + params.pageId, + { + ...noCacheFetchOptions, + } + ); + cacheTag(...getCacheTagsFromResponse(res)); + cacheLife('days'); + return res.data; + } + ); + }); + } +); + +/** + * List all the meta links for a given page in a change request. + */ +const listChangeRequestPageMetaLinks = cache( + async ( + input: DataFetcherInput, + params: { spaceId: string; changeRequestId: string; pageId: string } + ) => { + 'use cache'; + return wrapCacheDataFetcherError(async () => { + return trace( + `listChangeRequestPageMetaLinks(${params.spaceId}, ${params.changeRequestId}, ${params.pageId})`, + async () => { + const api = apiClient(input); + const res = await api.spaces.listChangeRequestPageMetaLinks( + params.spaceId, + params.changeRequestId, + params.pageId, + { + ...noCacheFetchOptions, + } + ); + cacheTag(...getCacheTagsFromResponse(res)); + cacheLife('days'); + return res.data; + } + ); + }); + } +); + /** * Create a new API client. */ diff --git a/packages/gitbook/src/lib/data/types.ts b/packages/gitbook/src/lib/data/types.ts index 20e77ff1f3..ceeb7d8661 100644 --- a/packages/gitbook/src/lib/data/types.ts +++ b/packages/gitbook/src/lib/data/types.ts @@ -182,4 +182,21 @@ export interface GitBookDataFetcher { revisionId: string; pageId: string; }): Promise>; + + /** + * List change request meta links. + */ + listChangeRequestPageMetaLinks(params: { + spaceId: string; + changeRequestId: string; + pageId: string; + }): Promise>; + + /** + * List space meta links. + */ + listSpacePageMetaLinks(params: { + spaceId: string; + pageId: string; + }): Promise>; }