Skip to content

Commit d8a4ecd

Browse files
authored
Pass site url as context to listSiteSpaces API (#2339)
1 parent 65c7968 commit d8a4ecd

File tree

4 files changed

+47
-10
lines changed

4 files changed

+47
-10
lines changed

src/app/(space)/fetch.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export function getContentPointer(): ContentPointer | SiteContentPointer {
4040
if (siteId) {
4141
const organizationId = headerSet.get('x-gitbook-content-organization');
4242
const siteSpaceId = headerSet.get('x-gitbook-content-site-space');
43+
const siteUrl = headerSet.get('x-gitbook-content-url');
4344
if (!organizationId) {
4445
throw new Error('Missing site content headers');
4546
}
@@ -48,6 +49,7 @@ export function getContentPointer(): ContentPointer | SiteContentPointer {
4849
siteId,
4950
spaceId,
5051
siteSpaceId: siteSpaceId ?? undefined,
52+
siteUrl: siteUrl ?? undefined,
5153
organizationId,
5254
revisionId: headerSet.get('x-gitbook-content-revision') ?? undefined,
5355
changeRequestId: headerSet.get('x-gitbook-content-changerequest') ?? undefined,
@@ -71,7 +73,10 @@ export async function fetchSpaceData() {
7173

7274
const [{ space, contentTarget, pages, customization, scripts }, parentSite] = await Promise.all(
7375
'siteId' in content
74-
? [getCurrentSiteData(content), fetchParentSite(content.organizationId, content.siteId)]
76+
? [
77+
getCurrentSiteData(content),
78+
fetchParentSite(content.organizationId, content.siteId, content.siteUrl),
79+
]
7580
: [getSpaceData(content)],
7681
);
7782

@@ -102,7 +107,7 @@ export async function fetchPageData(params: PagePathParams | PageIdParams) {
102107
const page = await resolvePage(contentTarget, pages, params);
103108
const [parent, document] = await Promise.all([
104109
'siteId' in content
105-
? fetchParentSite(content.organizationId, content.siteId)
110+
? fetchParentSite(content.organizationId, content.siteId, content.siteUrl)
106111
: fetchParentCollection(space),
107112
page?.page.documentId ? getDocument(space.id, page.page.documentId) : null,
108113
]);
@@ -173,10 +178,14 @@ async function fetchParentCollection(space: Space) {
173178
return { parent: collection, spaces };
174179
}
175180

176-
async function fetchParentSite(organizationId: string, siteId: string) {
181+
async function fetchParentSite(
182+
organizationId: string,
183+
siteId: string,
184+
siteUrl: string | undefined,
185+
) {
177186
const [site, siteSpaces] = await Promise.all([
178187
getSite(organizationId, siteId),
179-
getSiteSpaces(organizationId, siteId),
188+
getSiteSpaces(organizationId, siteId, siteUrl ?? null),
180189
]);
181190

182191
const spaces: Record<string, Space> = {};

src/components/Search/server-actions.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export async function searchParentContent(
7676
api.searchParentContent(parent.id, query),
7777
parent.object === 'collection' ? api.getCollectionSpaces(parent.id) : null,
7878
parent.object === 'site' && 'organizationId' in pointer
79-
? api.getSiteSpaces(pointer.organizationId, parent.id)
79+
? api.getSiteSpaces(pointer.organizationId, parent.id, pointer.siteUrl ?? null)
8080
: null,
8181
]);
8282

src/lib/api.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ export interface SiteContentPointer extends ContentPointer {
4747
* ID of the siteSpace can be undefined when rendering in multi-id mode (for site previews)
4848
*/
4949
siteSpaceId: string | undefined;
50+
/**
51+
* URL of the site content that was used for lookup. Only set for `multi` and `multi-path` modes
52+
* where an URL is involved in the lookup/resolution
53+
*/
54+
siteUrl: string | undefined;
5055
}
5156

5257
/**
@@ -126,6 +131,8 @@ export type PublishedContentWithCache =
126131
| ((PublishedContentLookup | PublishedSiteContentLookup) & {
127132
cacheMaxAge?: number;
128133
cacheTags?: string[];
134+
/** Published content URL that was used for lookup */
135+
contentUrl?: string;
129136
})
130137
| {
131138
error: {
@@ -235,6 +242,7 @@ export const getPublishedContentByUrl = cache(
235242
...response.data,
236243
cacheMaxAge: parsed.ttl,
237244
cacheTags: parsed.tags,
245+
contentUrl: url,
238246
};
239247
return {
240248
tags: [
@@ -736,12 +744,28 @@ export const getSite = cache(
736744
*/
737745
export const getSiteSpaces = cache(
738746
'api.getSiteSpaces',
739-
async (organizationId: string, siteId: string, options: CacheFunctionOptions) => {
747+
async (
748+
organizationId: string,
749+
siteId: string,
750+
/**
751+
* Additional site URL that can be used as context to resolve site space published urls
752+
*/
753+
siteUrlContext: string | null,
754+
options: CacheFunctionOptions,
755+
) => {
740756
const response = await getAll((params) =>
741-
api().orgs.listSiteSpaces(organizationId, siteId, params, {
742-
...noCacheFetchOptions,
743-
signal: options.signal,
744-
}),
757+
api().orgs.listSiteSpaces(
758+
organizationId,
759+
siteId,
760+
{
761+
...params,
762+
context: siteUrlContext ?? undefined,
763+
},
764+
{
765+
...noCacheFetchOptions,
766+
signal: options.signal,
767+
},
768+
),
745769
);
746770

747771
return cacheResponse(response, {

src/middleware.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@ export async function middleware(request: NextRequest) {
209209
headers.set('x-gitbook-content-site-space', resolved.siteSpace);
210210
}
211211
}
212+
if (resolved.contentUrl) {
213+
headers.set('x-gitbook-content-url', resolved.contentUrl);
214+
}
212215
if (resolved.revision) {
213216
headers.set('x-gitbook-content-revision', resolved.revision);
214217
}
@@ -659,6 +662,7 @@ async function lookupSpaceByAPI(
659662
apiToken: data.apiToken,
660663
cacheMaxAge: data.cacheMaxAge,
661664
cacheTags: data.cacheTags,
665+
contentUrl: data.contentUrl,
662666
...('site' in data
663667
? { site: data.site, siteSpace: data.siteSpace, organization: data.organization }
664668
: {}),

0 commit comments

Comments
 (0)