Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/odd-humans-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"gitbook-v2": patch
---

Fallback to the default section if one is not passed in the resolution context.
74 changes: 58 additions & 16 deletions packages/gitbook-v2/src/lib/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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(
Expand All @@ -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 = (() => {
Expand Down Expand Up @@ -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');
Expand Down
Loading