diff --git a/packages/sitecore-jss-nextjs/src/services/graphql-sitemap-service.test.ts b/packages/sitecore-jss-nextjs/src/services/graphql-sitemap-service.test.ts index 2380a49d57..cc92c0a3ef 100644 --- a/packages/sitecore-jss-nextjs/src/services/graphql-sitemap-service.test.ts +++ b/packages/sitecore-jss-nextjs/src/services/graphql-sitemap-service.test.ts @@ -318,6 +318,41 @@ describe('GraphQLSitemapService', () => { expect(sitemap).to.deep.equal(sitemapServiceResult); }); + it('should use a jssTemplateId, if provided', async () => { + const jssAppTemplateId = '{de397294-cfcc-4795-847e-442416d0617b}'; + const randomId = '{5a4e6edc-4518-4afb-afdc-9fa22ec4eb91}'; + + nock(endpoint) + .post('/', (body) => body.variables.jssAppTemplateId === jssAppTemplateId) + .reply(200, { + data: { + layout: { + homePage: { + rootItem: [ + { + id: randomId, + }, + ], + }, + }, + }, + }); + + nock(endpoint) + .post('/', (body) => body.variables.rootItemId === randomId) + .reply(200, sitemapQueryResult); + + const service = new GraphQLSitemapService({ + endpoint, + apiKey, + siteName, + jssAppTemplateId, + }); + + const sitemap = await service.fetchSSGSitemap(['ua']); + expect(sitemap).to.deep.equal(sitemapServiceResult); + }); + it('should throw error if SitemapQuery fails', async () => { mockRootItemIdRequest(); nock(endpoint) diff --git a/packages/sitecore-jss-nextjs/src/services/graphql-sitemap-service.ts b/packages/sitecore-jss-nextjs/src/services/graphql-sitemap-service.ts index 0e9e8e5e02..a8e365a788 100644 --- a/packages/sitecore-jss-nextjs/src/services/graphql-sitemap-service.ts +++ b/packages/sitecore-jss-nextjs/src/services/graphql-sitemap-service.ts @@ -76,6 +76,12 @@ export interface GraphQLSitemapServiceConfig extends SearchServiceConfig { * The API key to use for authentication. */ apiKey: string; + + /** + * Optional. The template ID of a JSS App to use when searching for the appRootId. + * @default '061cba1554744b918a0617903b102b82' (/sitecore/templates/Foundation/JavaScript Services/App) + */ + jssAppTemplateId?: string; } /** @@ -156,7 +162,12 @@ export class GraphQLSitemapService { // If the caller does not specify a root item ID, then we try to figure it out const rootItemId = this.options.rootItemId || - (await getAppRootId(this.graphQLClient, this.options.siteName, languages[0])); + (await getAppRootId( + this.graphQLClient, + this.options.siteName, + languages[0], + this.options.jssAppTemplateId + )); if (!rootItemId) { throw new Error(queryError); diff --git a/packages/sitecore-jss/src/i18n/graphql-dictionary-service.test.ts b/packages/sitecore-jss/src/i18n/graphql-dictionary-service.test.ts index 5a8ee13a42..c3e2b95f53 100644 --- a/packages/sitecore-jss/src/i18n/graphql-dictionary-service.test.ts +++ b/packages/sitecore-jss/src/i18n/graphql-dictionary-service.test.ts @@ -81,6 +81,42 @@ describe('GraphQLDictionaryService', () => { expect(result).to.have.all.keys('foo', 'bar'); }); + it('should use a jssTemplateId, if provided', async () => { + const jssAppTemplateId = '{71d608ca-ac9c-4f1c-8e0a-85a6946e30f8}'; + const randomId = '{412286b7-6d4f-4deb-80e9-108ee986c6e9}'; + + nock(endpoint) + .post('/', (body) => body.variables.jssAppTemplateId === jssAppTemplateId) + .reply(200, { + data: { + layout: { + homePage: { + rootItem: [ + { + id: randomId, + }, + ], + }, + }, + }, + }); + + nock(endpoint) + .post('/', (body) => body.variables.rootItemId === randomId) + .reply(200, dictionaryQueryResponse); + + const service = new GraphQLDictionaryService({ + endpoint, + apiKey, + siteName, + cacheEnabled: false, + jssAppTemplateId, + }); + + const result = await service.fetchDictionaryData('en'); + expect(result).to.have.all.keys('foo', 'bar'); + }); + it('should throw error if could not resolve rootItemId', async () => { nock(endpoint) .post('/', /AppRootQuery/) diff --git a/packages/sitecore-jss/src/i18n/graphql-dictionary-service.ts b/packages/sitecore-jss/src/i18n/graphql-dictionary-service.ts index 545d52efe0..fd3dab86f4 100644 --- a/packages/sitecore-jss/src/i18n/graphql-dictionary-service.ts +++ b/packages/sitecore-jss/src/i18n/graphql-dictionary-service.ts @@ -64,6 +64,12 @@ export interface GraphQLDictionaryServiceConfig extends SearchServiceConfig, Cac * @default '6d1cd89719364a3aa511289a94c2a7b1' (/sitecore/templates/System/Dictionary/Dictionary entry) */ dictionaryEntryTemplateId?: string; + + /** + * Optional. The template ID of a JSS App to use when searching for the appRootId. + * @default '061cba1554744b918a0617903b102b82' (/sitecore/templates/Foundation/JavaScript Services/App) + */ + jssAppTemplateId?: string; } /** @@ -111,7 +117,12 @@ export class GraphQLDictionaryService extends DictionaryServiceBase { // If the caller does not specify a root item ID, then we try to figure it out const rootItemId = this.options.rootItemId || - (await getAppRootId(this.graphQLClient, this.options.siteName, language)); + (await getAppRootId( + this.graphQLClient, + this.options.siteName, + language, + this.options.jssAppTemplateId + )); if (!rootItemId) { throw new Error(queryError);