Skip to content

Commit 8ad0657

Browse files
authored
Use site's share key as context while fetching site spaces (#2341)
1 parent d8a4ecd commit 8ad0657

File tree

7 files changed

+49
-34
lines changed

7 files changed

+49
-34
lines changed

bun.lockb

0 Bytes
Binary file not shown.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
],
2121
"dependencies": {
2222
"@geist-ui/icons": "^1.0.2",
23-
"@gitbook/api": "^0.50.0",
23+
"@gitbook/api": "^0.51.0",
2424
"@radix-ui/react-checkbox": "^1.0.4",
2525
"@radix-ui/react-popover": "^1.0.7",
2626
"@sentry/nextjs": "^7.94.1",

packages/react-contentkit/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"exports": "./src/index.ts",
44
"dependencies": {
55
"classnames": "^2.5.1",
6-
"@gitbook/api": "^0.50.0",
6+
"@gitbook/api": "^0.51.0",
77
"assert-never": "^1.2.1"
88
},
99
"peerDependencies": {

src/app/(space)/fetch.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +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');
43+
const siteShareKey = headerSet.get('x-gitbook-content-site-share-key');
4444
if (!organizationId) {
4545
throw new Error('Missing site content headers');
4646
}
@@ -49,7 +49,7 @@ export function getContentPointer(): ContentPointer | SiteContentPointer {
4949
siteId,
5050
spaceId,
5151
siteSpaceId: siteSpaceId ?? undefined,
52-
siteUrl: siteUrl ?? undefined,
52+
siteShareKey: siteShareKey ?? undefined,
5353
organizationId,
5454
revisionId: headerSet.get('x-gitbook-content-revision') ?? undefined,
5555
changeRequestId: headerSet.get('x-gitbook-content-changerequest') ?? undefined,
@@ -75,7 +75,11 @@ export async function fetchSpaceData() {
7575
'siteId' in content
7676
? [
7777
getCurrentSiteData(content),
78-
fetchParentSite(content.organizationId, content.siteId, content.siteUrl),
78+
fetchParentSite({
79+
organizationId: content.organizationId,
80+
siteId: content.siteId,
81+
siteShareKey: content.siteShareKey,
82+
}),
7983
]
8084
: [getSpaceData(content)],
8185
);
@@ -107,7 +111,11 @@ export async function fetchPageData(params: PagePathParams | PageIdParams) {
107111
const page = await resolvePage(contentTarget, pages, params);
108112
const [parent, document] = await Promise.all([
109113
'siteId' in content
110-
? fetchParentSite(content.organizationId, content.siteId, content.siteUrl)
114+
? fetchParentSite({
115+
organizationId: content.organizationId,
116+
siteId: content.siteId,
117+
siteShareKey: content.siteShareKey,
118+
})
111119
: fetchParentCollection(space),
112120
page?.page.documentId ? getDocument(space.id, page.page.documentId) : null,
113121
]);
@@ -178,14 +186,15 @@ async function fetchParentCollection(space: Space) {
178186
return { parent: collection, spaces };
179187
}
180188

181-
async function fetchParentSite(
182-
organizationId: string,
183-
siteId: string,
184-
siteUrl: string | undefined,
185-
) {
189+
async function fetchParentSite(args: {
190+
organizationId: string;
191+
siteId: string;
192+
siteShareKey: string | undefined;
193+
}) {
194+
const { organizationId, siteId, siteShareKey } = args;
186195
const [site, siteSpaces] = await Promise.all([
187196
getSite(organizationId, siteId),
188-
getSiteSpaces(organizationId, siteId, siteUrl ?? null),
197+
getSiteSpaces({ organizationId, siteId, siteShareKey }),
189198
]);
190199

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

src/components/Search/server-actions.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,11 @@ 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, pointer.siteUrl ?? null)
79+
? api.getSiteSpaces({
80+
organizationId: pointer.organizationId,
81+
siteId: parent.id,
82+
siteShareKey: pointer.siteShareKey,
83+
})
8084
: null,
8185
]);
8286

src/lib/api.ts

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ export interface SiteContentPointer extends ContentPointer {
4848
*/
4949
siteSpaceId: string | undefined;
5050
/**
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
51+
* Share key of the site that was used in lookup. Only set for `multi` and `multi-path` modes
52+
* where an URL with the share-link key is involved in the lookup/resolution.
5353
*/
54-
siteUrl: string | undefined;
54+
siteShareKey: string | undefined;
5555
}
5656

5757
/**
@@ -131,8 +131,6 @@ export type PublishedContentWithCache =
131131
| ((PublishedContentLookup | PublishedSiteContentLookup) & {
132132
cacheMaxAge?: number;
133133
cacheTags?: string[];
134-
/** Published content URL that was used for lookup */
135-
contentUrl?: string;
136134
})
137135
| {
138136
error: {
@@ -242,7 +240,6 @@ export const getPublishedContentByUrl = cache(
242240
...response.data,
243241
cacheMaxAge: parsed.ttl,
244242
cacheTags: parsed.tags,
245-
contentUrl: url,
246243
};
247244
return {
248245
tags: [
@@ -745,21 +742,21 @@ export const getSite = cache(
745742
export const getSiteSpaces = cache(
746743
'api.getSiteSpaces',
747744
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,
745+
args: {
746+
organizationId: string;
747+
siteId: string;
748+
/** Site share key that can be used as context to resolve site space published urls */
749+
siteShareKey: string | undefined;
750+
},
754751
options: CacheFunctionOptions,
755752
) => {
756753
const response = await getAll((params) =>
757754
api().orgs.listSiteSpaces(
758-
organizationId,
759-
siteId,
755+
args.organizationId,
756+
args.siteId,
760757
{
761758
...params,
762-
context: siteUrlContext ?? undefined,
759+
...(args.siteShareKey ? { shareKey: args.siteShareKey } : {}),
763760
},
764761
{
765762
...noCacheFetchOptions,
@@ -771,7 +768,7 @@ export const getSiteSpaces = cache(
771768
return cacheResponse(response, {
772769
revalidateBefore: 60 * 60,
773770
data: response.data.items.map((siteSpace) => siteSpace),
774-
tags: [getAPICacheTag({ tag: 'site', site: siteId })],
771+
tags: [getAPICacheTag({ tag: 'site', site: args.siteId })],
775772
});
776773
},
777774
);

src/middleware.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,11 @@ export async function middleware(request: NextRequest) {
208208
if (resolved.siteSpace) {
209209
headers.set('x-gitbook-content-site-space', resolved.siteSpace);
210210
}
211+
if (resolved.shareKey) {
212+
headers.set('x-gitbook-content-site-share-key', resolved.shareKey);
213+
}
211214
}
212-
if (resolved.contentUrl) {
213-
headers.set('x-gitbook-content-url', resolved.contentUrl);
214-
}
215+
215216
if (resolved.revision) {
216217
headers.set('x-gitbook-content-revision', resolved.revision);
217218
}
@@ -662,9 +663,13 @@ async function lookupSpaceByAPI(
662663
apiToken: data.apiToken,
663664
cacheMaxAge: data.cacheMaxAge,
664665
cacheTags: data.cacheTags,
665-
contentUrl: data.contentUrl,
666666
...('site' in data
667-
? { site: data.site, siteSpace: data.siteSpace, organization: data.organization }
667+
? {
668+
site: data.site,
669+
siteSpace: data.siteSpace,
670+
organization: data.organization,
671+
shareKey: data.shareKey,
672+
}
668673
: {}),
669674
} as PublishedContentWithCache;
670675
});

0 commit comments

Comments
 (0)