diff --git a/.changeset/shiny-clouds-float.md b/.changeset/shiny-clouds-float.md new file mode 100644 index 0000000000..158a74732e --- /dev/null +++ b/.changeset/shiny-clouds-float.md @@ -0,0 +1,5 @@ +--- +"gitbook-docs": patch +--- + +Refactor Strapi guide fetching to use server-side filtering using Strapi 5 capabilities. diff --git a/packages/gitbook-docs/src/helpers/strapiQuery.ts b/packages/gitbook-docs/src/helpers/strapiQuery.ts index 3402c26681..e46fd793ad 100644 --- a/packages/gitbook-docs/src/helpers/strapiQuery.ts +++ b/packages/gitbook-docs/src/helpers/strapiQuery.ts @@ -232,7 +232,6 @@ const solutionListPageQueryParams = { ...STRAPI_DEFAULT_PAGINATION, }; -export const guidesQueryString = qs.stringify(guidesQueryParams); export const solutionsQueryString = qs.stringify(solutionsQueryParams); export const productsQueryString = qs.stringify(productsQueryParams); export const apisDataQueryString = qs.stringify(apisDataQueryParams); @@ -244,6 +243,28 @@ export const solutionListPageQueryString = qs.stringify( solutionListPageQueryParams ); +/** + * Generate query string for guides with optional dirName filtering. + * Uses deep filtering on the 'versions' component. + */ +export function getGuidesQueryString(dirNames?: readonly string[]): string { + const params = { + ...guidesQueryParams, + ...(dirNames && dirNames.length > 0 + ? { + filters: { + versions: { + dirName: { + $in: dirNames, + }, + }, + }, + } + : {}), + }; + return qs.stringify(params); +} + /** * Generate query strings with optional dirName filtering. * @@ -262,12 +283,12 @@ export function getSolutionsQueryString(dirNames?: readonly string[]): string { ...solutionsQueryParams, ...(dirNames && dirNames.length > 0 ? { - filters: { - dirName: { - $in: dirNames, - }, + filters: { + dirName: { + $in: dirNames, }, - } + }, + } : {}), }; return qs.stringify(params); @@ -280,12 +301,12 @@ export function getReleaseNotesQueryString( ...releaseNotesQueryParams, ...(dirNames && dirNames.length > 0 ? { - filters: { - dirName: { - $in: dirNames, - }, + filters: { + dirName: { + $in: dirNames, }, - } + }, + } : {}), }; return qs.stringify(params); diff --git a/packages/gitbook-docs/src/scripts/deleteUnusedDirectories.ts b/packages/gitbook-docs/src/scripts/deleteUnusedDirectories.ts index 227bc8ff1b..53538c68b4 100644 --- a/packages/gitbook-docs/src/scripts/deleteUnusedDirectories.ts +++ b/packages/gitbook-docs/src/scripts/deleteUnusedDirectories.ts @@ -21,7 +21,7 @@ import { import { S3Client } from '@aws-sdk/client-s3'; import path from 'path'; import { - guidesQueryString, + getGuidesQueryString, releaseNotesQueryString, solutionsQueryString, } from '../helpers/strapiQuery'; @@ -49,7 +49,7 @@ async function fetchAllDirNamesFromStrapi(): Promise<{ dirNames: string[] }> { const [guidesResult, solutionsResult, releaseNotesResult] = await Promise.all( [ // Guides with full populate - fetchFromStrapi(`api/guides?${guidesQueryString}`), + fetchFromStrapi(`api/guides?${getGuidesQueryString()}`), // Solutions with full populate fetchFromStrapi(`api/solutions/?${solutionsQueryString}`), // Release notes with full populate diff --git a/packages/gitbook-docs/src/scripts/syncAllMetadata.ts b/packages/gitbook-docs/src/scripts/syncAllMetadata.ts index cc7f26acbb..7462f27b43 100644 --- a/packages/gitbook-docs/src/scripts/syncAllMetadata.ts +++ b/packages/gitbook-docs/src/scripts/syncAllMetadata.ts @@ -41,9 +41,9 @@ import { } from '../helpers/strapiTypes'; import { apisDataQueryString, + getGuidesQueryString, getReleaseNotesQueryString, getSolutionsQueryString, - guidesQueryString, productsQueryString, } from '../helpers/strapiQuery'; import { compact } from 'lodash'; @@ -153,8 +153,7 @@ async function fetchAllStrapiData(): Promise { console.log('Fetching all data from Strapi...'); if (DIR_NAMES_FILTER) { console.log( - `Applying dirName filter: ${DIR_NAMES_FILTER.join(', ')} (${ - DIR_NAMES_FILTER.length + `Applying dirName filter: ${DIR_NAMES_FILTER.join(', ')} (${DIR_NAMES_FILTER.length } directories)` ); } @@ -166,10 +165,10 @@ async function fetchAllStrapiData(): Promise { productsResult, apisDataResult, ] = await Promise.all([ - // Guides with full populate - // NOTE: Cannot filter by versions.dirName server-side due to Strapi v4 component array limitation - // Client-side filtering will be applied later in processGuidesMetadata - fetchFromStrapi(`api/guides?${guidesQueryString}`), + // Guides with full populate and optional dirName filtering + fetchFromStrapi( + `api/guides?${getGuidesQueryString(DIR_NAMES_FILTER)}` + ), // Solutions with dirName filter (if provided) fetchFromStrapi( `api/solutions/?${getSolutionsQueryString(DIR_NAMES_FILTER)}`