Compare commits

...

7 Commits

Author SHA1 Message Date
Steven Hall 1e6fb0e54d changeset 2025-04-23 14:04:13 +01:00
Steven Hall 3fb7b22d8e Format 2025-04-23 14:00:05 +01:00
Steven Hall 5b5c9b3853 Merge remote-tracking branch 'origin/main' into stevenh/inc-54-2 2025-04-23 13:59:57 +01:00
Steven Hall a945a36d28 Fallback to the default section if one is not passed in the resolution context. 2025-04-23 13:59:27 +01:00
Steven Hall 5bf99cfcf3 changeset 2025-04-23 12:34:08 +01:00
Steven Hall 40916b8523 Message 2025-04-23 12:33:45 +01:00
Steven Hall 446e0f7321 Improve error messages around undefined site sections. 2025-04-23 12:30:43 +01:00
2 changed files with 62 additions and 15 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"gitbook-v2": patch
---
Fallback to the default section if one is not passed in the resolution context.
+57 -15
View File
@@ -239,12 +239,12 @@ export async function fetchSiteContextByIds(
...(customizations.site?.title ? { title: customizations.site.title } : {}),
};
const sections = ids.siteSection
? parseSiteSectionsAndGroups(siteStructure, ids.siteSection)
: null;
// Parse the current siteSpace and siteSpaces based on the site structure type.
const { siteSpaces, siteSpace }: { siteSpaces: SiteSpace[]; siteSpace: SiteSpace } = (() => {
// Parse the current siteSpace, siteSpaces, and sections based on the site structure type.
const {
siteSpaces,
siteSpace,
sections,
}: { siteSpaces: SiteSpace[]; siteSpace: SiteSpace; sections: SiteSections | null } = (() => {
if (siteStructure.type === 'siteSpaces') {
const siteSpaces = siteStructure.structure;
const siteSpace = siteSpaces.find((siteSpace) => siteSpace.id === ids.siteSpace);
@@ -255,15 +255,54 @@ export async function fetchSiteContextByIds(
);
}
return { siteSpaces, siteSpace };
return { siteSpaces, siteSpace, sections: null };
}
if (siteStructure.type === 'sections') {
assert(
sections,
`cannot find site space "${ids.siteSpace}" because parsed sections are missing siteStructure.type="sections" siteSection="${ids.siteSection}"`
);
/**
* Workaround for #inc-56-sites-are-crashing-with-application-error-a-client-side-exception-ha
*
* If a site structure is set up as sections, we should always be given a siteSection in the ids. However,
* in the incident we noticed it is sometimes undefined - perhaps due to caching.
*
* This workaround will pick the default section in the site structure if no explicit is provided. It should
* be eventually removed and replaced with an assert.
*/
const currentSectionId = (() => {
if (ids.siteSection) {
return ids.siteSection;
}
let defaultSectionId: string | undefined;
for (const sectionOrGroup of siteStructure.structure) {
if (sectionOrGroup.object === 'site-section') {
defaultSectionId = sectionOrGroup.id;
break;
}
if (sectionOrGroup.object === 'site-section-group') {
const defaultSection = sectionOrGroup.sections.find(
(group) => group.default
);
if (defaultSection) {
defaultSectionId = defaultSection.id;
break;
}
}
}
if (!defaultSectionId) {
throw new Error(
`No default section found in structure type="sections" for site "${ids.site}"`
);
}
return defaultSectionId;
})();
const sections = parseSiteSectionsAndGroups(siteStructure, currentSectionId);
const currentSection = sections.current;
const siteSpaces = currentSection.siteSpaces;
const siteSpace = currentSection.siteSpaces.find(
@@ -276,11 +315,14 @@ export async function fetchSiteContextByIds(
);
}
return { siteSpaces, siteSpace };
return { siteSpaces, siteSpace, sections };
}
// @ts-expect-error
assertNever(siteStructure, `cannot handle site structure of type ${siteStructure.type}`);
assertNever(
siteStructure,
// @ts-expect-error
`cannot handle site structure of type ${siteStructure.type}`
);
})();
const customization = (() => {
@@ -404,7 +446,7 @@ export function checkIsRootSiteContext(context: GitBookSiteContext): boolean {
}
}
function parseSiteSectionsAndGroups(structure: SiteStructure, siteSectionId: string) {
function parseSiteSectionsAndGroups(structure: SiteStructure, siteSectionId: string): SiteSections {
const sectionsAndGroups = getSiteStructureSections(structure, { ignoreGroups: false });
const section = parseCurrentSection(structure, siteSectionId);
assert(section, 'A section must be defined when there are multiple sections');