From 4b163cd45fd32d5655de005b08152e8714fb63c9 Mon Sep 17 00:00:00 2001 From: Addy Pathania Date: Fri, 7 Oct 2022 16:42:30 -0400 Subject: [PATCH 1/3] fix rootItemId for different language version --- packages/sitecore-jss/src/graphql/app-root-query.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/sitecore-jss/src/graphql/app-root-query.ts b/packages/sitecore-jss/src/graphql/app-root-query.ts index cd4ca6a7f8..2b4184ebfb 100644 --- a/packages/sitecore-jss/src/graphql/app-root-query.ts +++ b/packages/sitecore-jss/src/graphql/app-root-query.ts @@ -65,12 +65,20 @@ export async function getAppRootId( debug.dictionary('fetching site root for %s %s', language, siteName); - const fetchResponse = await client.request(appRootQuery, { + let fetchResponse = await client.request(appRootQuery, { jssAppTemplateId: jssAppTemplateId || SitecoreTemplateId.JssApp, siteName, language, }); + if (!fetchResponse?.layout?.homePage?.rootItem?.length) { + fetchResponse = await client.request(appRootQuery, { + jssAppTemplateId: jssAppTemplateId || SitecoreTemplateId.JssApp, + siteName, + language: 'en', + }); + } + if (!fetchResponse?.layout?.homePage?.rootItem?.length) { return null; } From a637f4be2c527ceaaa7a4feb5cb5177a8a50cfbc Mon Sep 17 00:00:00 2001 From: Addy Pathania Date: Thu, 13 Oct 2022 12:26:34 -0400 Subject: [PATCH 2/3] updated test, moved debug logs statements --- packages/sitecore-jss/src/graphql/app-root-query.ts | 6 +----- packages/sitecore-jss/src/graphql/graphql.test.ts | 2 +- .../sitecore-jss/src/i18n/graphql-dictionary-service.ts | 2 ++ 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/packages/sitecore-jss/src/graphql/app-root-query.ts b/packages/sitecore-jss/src/graphql/app-root-query.ts index 2b4184ebfb..9713ac09f3 100644 --- a/packages/sitecore-jss/src/graphql/app-root-query.ts +++ b/packages/sitecore-jss/src/graphql/app-root-query.ts @@ -1,6 +1,5 @@ import { GraphQLClient } from './../graphql-request-client'; import { SitecoreTemplateId } from '../constants'; -import debug from '../debug'; /** @private */ export const siteNameError = 'The site name must be a non-empty string'; @@ -62,16 +61,13 @@ export async function getAppRootId( if (!language) { throw new RangeError(languageError); } - - debug.dictionary('fetching site root for %s %s', language, siteName); - let fetchResponse = await client.request(appRootQuery, { jssAppTemplateId: jssAppTemplateId || SitecoreTemplateId.JssApp, siteName, language, }); - if (!fetchResponse?.layout?.homePage?.rootItem?.length) { + if (!fetchResponse?.layout?.homePage?.rootItem?.length && language !== 'en') { fetchResponse = await client.request(appRootQuery, { jssAppTemplateId: jssAppTemplateId || SitecoreTemplateId.JssApp, siteName, diff --git a/packages/sitecore-jss/src/graphql/graphql.test.ts b/packages/sitecore-jss/src/graphql/graphql.test.ts index 35b120d80d..22adc0b0cd 100644 --- a/packages/sitecore-jss/src/graphql/graphql.test.ts +++ b/packages/sitecore-jss/src/graphql/graphql.test.ts @@ -44,7 +44,7 @@ describe('graphql', () => { }, }); - const result = await getAppRootId(client, 'siteName', 'language'); + const result = await getAppRootId(client, 'siteName', 'en'); expect(result).to.be.null; }); }); diff --git a/packages/sitecore-jss/src/i18n/graphql-dictionary-service.ts b/packages/sitecore-jss/src/i18n/graphql-dictionary-service.ts index ba5555d7c7..50be55aee1 100644 --- a/packages/sitecore-jss/src/i18n/graphql-dictionary-service.ts +++ b/packages/sitecore-jss/src/i18n/graphql-dictionary-service.ts @@ -114,6 +114,8 @@ export class GraphQLDictionaryService extends DictionaryServiceBase { return cachedValue; } + debug.dictionary('fetching site root for %s %s', language, this.options.siteName); + // If the caller does not specify a root item ID, then we try to figure it out const rootItemId = this.options.rootItemId || From 8f17088eb768002575adf79ccaf75784d0d7ca4f Mon Sep 17 00:00:00 2001 From: Addy Pathania Date: Fri, 14 Oct 2022 12:35:34 -0400 Subject: [PATCH 3/3] added more test cases for root id --- .../sitecore-jss/src/graphql/graphql.test.ts | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/packages/sitecore-jss/src/graphql/graphql.test.ts b/packages/sitecore-jss/src/graphql/graphql.test.ts index 22adc0b0cd..ccfaf90743 100644 --- a/packages/sitecore-jss/src/graphql/graphql.test.ts +++ b/packages/sitecore-jss/src/graphql/graphql.test.ts @@ -31,6 +31,64 @@ describe('graphql', () => { expect(result).to.equal('GUIDGUIDGUID'); }); + it('valid root id using fallback language as en', async () => { + nock(endpoint, { reqheaders: { sc_apikey: apiKey } }) + .post('/', (body) => { + return body.variables.language === 'language'; + }) + .reply(200, { + data: { + layout: { + homePage: { + rootItem: [], + }, + }, + }, + }); + + nock(endpoint, { reqheaders: { sc_apikey: apiKey } }) + .post('/', (body) => { + return body.variables.language === 'en'; + }) + .reply(200, appRootQueryResponse); + + const result = await getAppRootId(client, 'siteName', 'language'); + expect(result).to.equal('GUIDGUIDGUID'); + }); + + it('null for the passed language and for fallback language en', async () => { + nock(endpoint, { reqheaders: { sc_apikey: apiKey } }) + .post('/', (body) => { + return body.variables.language === 'language'; + }) + .reply(200, { + data: { + layout: { + homePage: { + rootItem: [], + }, + }, + }, + }); + + nock(endpoint, { reqheaders: { sc_apikey: apiKey } }) + .post('/', (body) => { + return body.variables.language === 'en'; + }) + .reply(200, { + data: { + layout: { + homePage: { + rootItem: [], + }, + }, + }, + }); + + const result = await getAppRootId(client, 'siteName', 'language'); + expect(result).to.be.null; + }); + it('null if no app root found', async () => { nock(endpoint, { reqheaders: { sc_apikey: apiKey } }) .post('/', queryNameFilter)