Skip to content

Commit b6826dc

Browse files
GraphQL sitemap now parses personalize data from site queries and adds it into returned paths
1 parent ddcd951 commit b6826dc

File tree

3 files changed

+97
-3
lines changed

3 files changed

+97
-3
lines changed

packages/sitecore-jss-nextjs/src/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ export {
55
HttpResponse,
66
AxiosDataFetcher,
77
AxiosDataFetcherConfig,
8-
enableDebug,
98
} from '@sitecore-jss/sitecore-jss';
109
export {
1110
isEditorActive,

packages/sitecore-jss-nextjs/src/services/graphql-sitemap-service.test.ts

+78
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,84 @@ describe('GraphQLSitemapService', () => {
170170
]);
171171
});
172172

173+
it('should return personalized paths when personalize data is returned', async () => {
174+
const lang1 = 'ua';
175+
176+
nock(endpoint)
177+
.post('/')
178+
.reply(200, {
179+
data: {
180+
site: {
181+
siteInfo: {
182+
routes: {
183+
total: 4,
184+
pageInfo: {
185+
hasNext: false,
186+
},
187+
results: [
188+
{
189+
path: '/',
190+
personalize: {
191+
variantIds: ['green'],
192+
},
193+
},
194+
{
195+
path: '/y1/y2/y3/y4',
196+
personalize: {
197+
variantIds: ['green', 'red', 'purple'],
198+
},
199+
},
200+
],
201+
},
202+
},
203+
},
204+
},
205+
});
206+
207+
const service = new GraphQLSitemapService({ endpoint, apiKey, siteName });
208+
const sitemap = await service.fetchSSGSitemap([lang1]);
209+
210+
expect(sitemap).to.deep.equal([
211+
{
212+
params: {
213+
path: [''],
214+
},
215+
locale: 'ua',
216+
},
217+
{
218+
params: {
219+
path: ['green'],
220+
},
221+
locale: 'ua',
222+
},
223+
{
224+
params: {
225+
path: ['y1', 'y2', 'y3', 'y4'],
226+
},
227+
locale: 'ua',
228+
},
229+
{
230+
params: {
231+
path: ['green', 'y1', 'y2', 'y3', 'y4'],
232+
},
233+
locale: 'ua',
234+
},
235+
{
236+
params: {
237+
path: ['red', 'y1', 'y2', 'y3', 'y4'],
238+
},
239+
locale: 'ua',
240+
},
241+
{
242+
params: {
243+
path: ['purple', 'y1', 'y2', 'y3', 'y4'],
244+
},
245+
locale: 'ua',
246+
},
247+
]);
248+
return expect(nock.isDone()).to.be.true;
249+
});
250+
173251
it('should work when multiple languages are requested', async () => {
174252
const lang1 = 'ua';
175253
const lang2 = 'da-DK';

packages/sitecore-jss-nextjs/src/services/graphql-sitemap-service.ts

+19-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ query SitemapQuery(
3131
}
3232
results: routesResult{
3333
path: routePath
34+
personalize: {
35+
variantIds
36+
}
3437
}
3538
}
3639
}
@@ -89,6 +92,10 @@ export interface SiteRouteQueryResult<T> {
8992
*/
9093
export type RouteListQueryResult = {
9194
path: string;
95+
96+
personalize: {
97+
variantIds: string[];
98+
};
9299
};
93100

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

210217
return this.fetchLanguageSitePaths(this.query, args).then((results) => {
211-
return results.map((item) =>
212-
formatStaticPath(item.path.replace(/^\/|\/$/g, '').split('/'), language)
218+
const aggregatedPaths: string[] = [];
219+
results.forEach((item) => {
220+
aggregatedPaths.push(item.path);
221+
if (item.personalize?.variantIds.length) {
222+
aggregatedPaths.push(
223+
...item.personalize?.variantIds.map((varId) => `/${varId}${item.path}`)
224+
);
225+
}
226+
});
227+
228+
return aggregatedPaths.map((item) =>
229+
formatStaticPath(item.replace(/^\/|\/$/g, '').split('/'), language)
213230
);
214231
});
215232
})

0 commit comments

Comments
 (0)