mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-16 07:35:16 +00:00
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1e6fb0e54d | |||
| 3fb7b22d8e | |||
| 5b5c9b3853 | |||
| a945a36d28 | |||
| 5bf99cfcf3 | |||
| 40916b8523 | |||
| 446e0f7321 |
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"gitbook-v2": patch
|
||||
---
|
||||
|
||||
Fallback to the default section if one is not passed in the resolution context.
|
||||
@@ -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');
|
||||
|
||||
Reference in New Issue
Block a user