Skip to content

Commit 26ab764

Browse files
committed
Use middleware to resolve siteSection
1 parent c054650 commit 26ab764

File tree

6 files changed

+50
-31
lines changed

6 files changed

+50
-31
lines changed

packages/gitbook/src/app/(site)/fetch.ts

+25-23
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,22 @@ export async function fetchContentData() {
3838
]);
3939

4040
const site = siteStructure.site;
41-
const spaces = siteStructure.spaces;
42-
const sections = siteStructure.sections;
4341

42+
const sections = siteStructure.sections;
43+
const section = siteStructure.sections?.find(section => section.id === content.siteSectionId);
44+
45+
const spaces = siteStructure.spaces ?? (section ? parseSpacesFromSiteSpaces(section.siteSpaces) : []);
46+
4447
// we grab the space attached to the parent as it contains overriden customizations
45-
const spaceRelativeToParent = spaces.find((space) => space.id === content.spaceId);
48+
const spaceRelativeToParent = spaces?.find((space) => space.id === content.spaceId);
4649

4750
return {
4851
content,
4952
contentTarget,
5053
space: spaceRelativeToParent ?? space,
5154
pages,
5255
sections,
53-
section: siteStructure.section,
56+
section,
5457
site,
5558
spaces,
5659
customization,
@@ -143,19 +146,30 @@ async function fetchSiteStructure(args: {
143146
siteShareKey: string | undefined;
144147
siteSpaceId: string | undefined;
145148
}) {
146-
const { organizationId, siteId, siteSpaceId} = args;
149+
const { organizationId, siteId, siteShareKey } = args;
147150
const [orgSite, siteStructure, siteParentCustomizations] = await Promise.all([
148151
getSite(organizationId, siteId),
149-
getSiteStructure({ organizationId, siteId }),
152+
getSiteStructure({ organizationId, siteId, siteShareKey }),
150153
getCurrentSiteCustomization({ organizationId, siteId, siteSpaceId: undefined }),
151154
]);
152155

153-
const siteSections = siteStructure.type === 'sections' && siteStructure.structure ? siteStructure.structure : [];
156+
const siteSections = siteStructure.type === 'sections' && siteStructure.structure ? siteStructure.structure : null;
157+
const siteSpaces = siteStructure.type === 'siteSpaces' && siteStructure.structure ? parseSpacesFromSiteSpaces(siteStructure.structure) : null;
158+
159+
// override the title with the customization title
160+
const site = {
161+
...orgSite,
162+
...(siteParentCustomizations?.title ? { title: siteParentCustomizations.title } : {}),
163+
};
154164

155-
const section = siteSections.find(section => section.id === siteSpaceId || section.siteSpaces.find(siteSpace => siteSpace.id === siteSpaceId));
165+
return {
166+
site,
167+
spaces: siteSpaces,
168+
sections: siteSections,
169+
};
170+
}
156171

157-
const siteSpaces: SiteSpace[] = siteStructure.type === 'siteSpaces' && siteStructure.structure ? siteStructure.structure : section?.siteSpaces ?? [];
158-
172+
function parseSpacesFromSiteSpaces(siteSpaces: SiteSpace[]) {
159173
const spaces: Record<string, Space> = {};
160174
siteSpaces.forEach((siteSpace) => {
161175
spaces[siteSpace.space.id] = {
@@ -167,19 +181,7 @@ async function fetchSiteStructure(args: {
167181
},
168182
};
169183
});
170-
171-
// override the title with the customization title
172-
const site = {
173-
...orgSite,
174-
...(siteParentCustomizations?.title ? { title: siteParentCustomizations.title } : {}),
175-
};
176-
177-
return {
178-
site,
179-
spaces: Object.values(spaces),
180-
sections: siteSections,
181-
section
182-
};
184+
return Object.values(spaces);
183185
}
184186

185187
/**

packages/gitbook/src/components/SiteSectionTabs/SiteSectionTab.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { tcls } from '@/lib/tailwind';
77

88
import { Button, Link } from '../primitives';
99

10-
export function SiteSectionTabs(props: { sections: SiteSection[]; section?: SiteSection; }) {
10+
export function SiteSectionTabs(props: { sections: SiteSection[]; section: SiteSection; }) {
1111
const { sections, section: currentSection } = props;
1212

1313
const tabs = sections.map((section) => ({ id: section.id, label: section.title, path: section.urls.published ?? '' }));

packages/gitbook/src/components/SpaceLayout/SpaceLayout.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export function SpaceLayout(props: {
3131
contentTarget: ContentTarget;
3232
space: Space;
3333
site: Site | null;
34-
sections: SiteSection[];
34+
sections: SiteSection[] | null;
3535
section?: SiteSection;
3636
spaces: Space[];
3737
customization: CustomizationSettings | SiteCustomizationSettings;
@@ -74,9 +74,9 @@ export function SpaceLayout(props: {
7474
customization={customization}
7575
/>
7676
<div className={tcls('scroll-nojump')}>
77-
<div className={tcls(CONTAINER_STYLE)}>
77+
{ sections && section ? <div className={tcls(CONTAINER_STYLE)}>
7878
<SiteSectionTabs sections={sections} section={section} />
79-
</div>
79+
</div> : null }
8080
<div
8181
className={tcls(
8282
'flex',

packages/gitbook/src/lib/api.ts

+15-4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ export interface SpaceContentPointer {
4545
export interface SiteContentPointer extends SpaceContentPointer {
4646
organizationId: string;
4747
siteId: string;
48+
/**
49+
* ID of the siteSection. When rendering a multi-section site. Can be undefined.
50+
*/
51+
siteSectionId: string | undefined;
4852
/**
4953
* ID of the siteSpace can be undefined when rendering in multi-id mode (for site previews)
5054
*/
@@ -739,18 +743,25 @@ export const getSiteSpaces = cache({
739743

740744
export const getSiteStructure = cache({
741745
name: 'api.getSiteStructure',
742-
tag: ({ organizationId, siteId }) => getAPICacheTag({ tag: 'site', site: siteId }),
746+
tag: ({ siteId }) => getAPICacheTag({ tag: 'site', site: siteId }),
743747
get: async (
744-
args: { organizationId: string; siteId: string },
748+
args: {
749+
organizationId: string;
750+
siteId: string,
751+
/** Site share key that can be used as context to resolve site space published urls */
752+
siteShareKey: string | undefined;
753+
},
745754
options: CacheFunctionOptions,
746755
) => {
747756
const response = await api().orgs.getSiteStructure(
748757
args.organizationId,
749758
args.siteId,
750-
{},
751759
{
752-
signal: options.signal,
760+
...(args.siteShareKey ? { shareKey: args.siteShareKey } : {}),
761+
},
762+
{
753763
...noCacheFetchOptions,
764+
signal: options.signal,
754765
},
755766
);
756767
return cacheResponse(response, {

packages/gitbook/src/lib/pointer.ts

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export function getSiteContentPointer(): SiteContentPointer {
1111
const siteId = headerSet.get('x-gitbook-content-site');
1212
const organizationId = headerSet.get('x-gitbook-content-organization');
1313
const siteSpaceId = headerSet.get('x-gitbook-content-site-space');
14+
const siteSectionId = headerSet.get('x-gitbook-content-site-section');
1415
const siteShareKey = headerSet.get('x-gitbook-content-site-share-key');
1516

1617
if (!spaceId || !siteId || !organizationId) {
@@ -22,6 +23,7 @@ export function getSiteContentPointer(): SiteContentPointer {
2223
const pointer: SiteContentPointer = {
2324
siteId,
2425
spaceId,
26+
siteSectionId: siteSectionId ?? undefined,
2527
siteSpaceId: siteSpaceId ?? undefined,
2628
siteShareKey: siteShareKey ?? undefined,
2729
organizationId,

packages/gitbook/src/middleware.ts

+4
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ export async function middleware(request: NextRequest) {
200200
if ('site' in resolved) {
201201
headers.set('x-gitbook-content-organization', resolved.organization);
202202
headers.set('x-gitbook-content-site', resolved.site);
203+
if (resolved.siteSection) {
204+
headers.set('x-gitbook-content-site-section', resolved.siteSection);
205+
}
203206
if (resolved.siteSpace) {
204207
headers.set('x-gitbook-content-site-space', resolved.siteSpace);
205208
}
@@ -208,6 +211,7 @@ export async function middleware(request: NextRequest) {
208211
}
209212
}
210213

214+
211215
// For tests, we make it possible to enable search indexation
212216
// using a query parameter.
213217
const xGitBookSearchIndexation =

0 commit comments

Comments
 (0)