Skip to content

Commit

Permalink
fix paginated results retrieval in sitemap paths service (#1112)
Browse files Browse the repository at this point in the history
  • Loading branch information
art-alexeyenko authored Jul 20, 2022
1 parent b0e4372 commit d737352
Showing 1 changed file with 40 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import { getPersonalizedRewrite } from '@sitecore-jss/sitecore-jss/personalize';

/** @private */
export const languageError = 'The list of languages cannot be empty';

/**
* @param siteName
* @param {string} siteName to inject into error text
* @private
*/
/** @private */
export function getSiteEmptyError(siteName: string) {
return `Site "${siteName}" does not exist or site item tree is missing`;
}
Expand Down Expand Up @@ -222,62 +223,63 @@ export class GraphQLSitemapService {
if (!languages.length) {
throw new RangeError(languageError);
}
const siteName = this.options.siteName;

const args: SiteRouteQueryVariables = {
siteName,
language: '',
pageSize: this.options.pageSize,
includedPaths: this.options.includedPaths,
excludedPaths: this.options.excludedPaths,
};

// Fetch paths using all locales
const paths = await Promise.all(
languages.map((language) => {
if (language === '') {
throw new RangeError(languageEmptyError);
}
args.language = language;
debug.sitemap('fetching sitemap data for %s', language);

return this.fetchLanguageSitePaths(this.query, args).then((results) => {
const formatPath = (path: string) =>
formatStaticPath(path.replace(/^\/|\/$/g, '').split('/'), language);
const aggregatedPaths: StaticPath[] = [];
results.forEach((item) => {
aggregatedPaths.push(formatPath(item.path));
// check for type safety's sake - personalize may be empty depending on query type
if (item.route?.personalization?.variantIds.length) {
aggregatedPaths.push(
...item.route?.personalization?.variantIds.map((varId) =>
formatPath(getPersonalizedRewrite(item.path, { variantId: varId }))
)
);
}
});

return aggregatedPaths;
});
return this.fetchLanguageSitePaths(language).then((results) =>
this.transformLanguageSitePaths(results, formatStaticPath, language)
);
})
);

// merge promises results into single result
return ([] as StaticPath[]).concat(...paths);
}

protected async fetchLanguageSitePaths(
query: string,
args: SiteRouteQueryVariables
): Promise<RouteListQueryResult[]> {
protected async transformLanguageSitePaths(
sitePaths: RouteListQueryResult[],
formatStaticPath: (path: string[], language: string) => StaticPath,
language: string
): Promise<StaticPath[]> {
const formatPath = (path: string) =>
formatStaticPath(path.replace(/^\/|\/$/g, '').split('/'), language);
const aggregatedPaths: StaticPath[] = [];

sitePaths.forEach((item) => {
aggregatedPaths.push(formatPath(item.path));
// check for type safety's sake - personalize may be empty depending on query type
if (item.route?.personalization?.variantIds.length) {
aggregatedPaths.push(
...item.route?.personalization?.variantIds.map((varId) =>
formatPath(getPersonalizedRewrite(item.path, { variantId: varId }))
)
);
}
});

return aggregatedPaths;
}

protected async fetchLanguageSitePaths(language: string): Promise<RouteListQueryResult[]> {
const args: SiteRouteQueryVariables = {
siteName: this.options.siteName,
language: language,
pageSize: this.options.pageSize,
includedPaths: this.options.includedPaths,
excludedPaths: this.options.excludedPaths,
};
let results: RouteListQueryResult[] = [];
let hasNext = true;
let after = '';

while (hasNext) {
const fetchResponse = await this.graphQLClient.request<
SiteRouteQueryResult<RouteListQueryResult>
>(query, {
>(this.query, {
...args,
after,
});
Expand All @@ -290,6 +292,7 @@ export class GraphQLSitemapService {
after = fetchResponse.site.siteInfo.routes?.pageInfo.endCursor;
}
}

return results;
}

Expand Down

0 comments on commit d737352

Please sign in to comment.