Skip to content

Commit

Permalink
GraphQL sitemap now parses personalize data from site queries and add…
Browse files Browse the repository at this point in the history
…s it into returned paths
  • Loading branch information
art-alexeyenko committed May 4, 2022
1 parent ddcd951 commit b6826dc
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 3 deletions.
1 change: 0 additions & 1 deletion packages/sitecore-jss-nextjs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export {
HttpResponse,
AxiosDataFetcher,
AxiosDataFetcherConfig,
enableDebug,
} from '@sitecore-jss/sitecore-jss';
export {
isEditorActive,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,84 @@ describe('GraphQLSitemapService', () => {
]);
});

it('should return personalized paths when personalize data is returned', async () => {
const lang1 = 'ua';

nock(endpoint)
.post('/')
.reply(200, {
data: {
site: {
siteInfo: {
routes: {
total: 4,
pageInfo: {
hasNext: false,
},
results: [
{
path: '/',
personalize: {
variantIds: ['green'],
},
},
{
path: '/y1/y2/y3/y4',
personalize: {
variantIds: ['green', 'red', 'purple'],
},
},
],
},
},
},
},
});

const service = new GraphQLSitemapService({ endpoint, apiKey, siteName });
const sitemap = await service.fetchSSGSitemap([lang1]);

expect(sitemap).to.deep.equal([
{
params: {
path: [''],
},
locale: 'ua',
},
{
params: {
path: ['green'],
},
locale: 'ua',
},
{
params: {
path: ['y1', 'y2', 'y3', 'y4'],
},
locale: 'ua',
},
{
params: {
path: ['green', 'y1', 'y2', 'y3', 'y4'],
},
locale: 'ua',
},
{
params: {
path: ['red', 'y1', 'y2', 'y3', 'y4'],
},
locale: 'ua',
},
{
params: {
path: ['purple', 'y1', 'y2', 'y3', 'y4'],
},
locale: 'ua',
},
]);
return expect(nock.isDone()).to.be.true;
});

it('should work when multiple languages are requested', async () => {
const lang1 = 'ua';
const lang2 = 'da-DK';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ query SitemapQuery(
}
results: routesResult{
path: routePath
personalize: {
variantIds
}
}
}
}
Expand Down Expand Up @@ -89,6 +92,10 @@ export interface SiteRouteQueryResult<T> {
*/
export type RouteListQueryResult = {
path: string;

personalize: {
variantIds: string[];
};
};

/**
Expand Down Expand Up @@ -208,8 +215,18 @@ export class GraphQLSitemapService {
debug.sitemap('fetching sitemap data for %s', language);

return this.fetchLanguageSitePaths(this.query, args).then((results) => {
return results.map((item) =>
formatStaticPath(item.path.replace(/^\/|\/$/g, '').split('/'), language)
const aggregatedPaths: string[] = [];
results.forEach((item) => {
aggregatedPaths.push(item.path);
if (item.personalize?.variantIds.length) {
aggregatedPaths.push(
...item.personalize?.variantIds.map((varId) => `/${varId}${item.path}`)
);
}
});

return aggregatedPaths.map((item) =>
formatStaticPath(item.replace(/^\/|\/$/g, '').split('/'), language)
);
});
})
Expand Down

0 comments on commit b6826dc

Please sign in to comment.