Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[sitecore-jss] rootItemId not resolvable for different language versions #1196

Merged
merged 3 commits into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions packages/sitecore-jss/src/graphql/app-root-query.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -62,15 +61,20 @@ export async function getAppRootId(
if (!language) {
throw new RangeError(languageError);
}

debug.dictionary('fetching site root for %s %s', language, siteName);

const fetchResponse = await client.request<AppRootQueryResult>(appRootQuery, {
let fetchResponse = await client.request<AppRootQueryResult>(appRootQuery, {
jssAppTemplateId: jssAppTemplateId || SitecoreTemplateId.JssApp,
siteName,
language,
});

if (!fetchResponse?.layout?.homePage?.rootItem?.length && language !== 'en') {
fetchResponse = await client.request<AppRootQueryResult>(appRootQuery, {
jssAppTemplateId: jssAppTemplateId || SitecoreTemplateId.JssApp,
siteName,
language: 'en',
});
}

if (!fetchResponse?.layout?.homePage?.rootItem?.length) {
return null;
}
Expand Down
60 changes: 59 additions & 1 deletion packages/sitecore-jss/src/graphql/graphql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -44,7 +102,7 @@ describe('graphql', () => {
},
});

const result = await getAppRootId(client, 'siteName', 'language');
const result = await getAppRootId(client, 'siteName', 'en');
addy-pathania marked this conversation as resolved.
Show resolved Hide resolved
expect(result).to.be.null;
});
});
Expand Down
2 changes: 2 additions & 0 deletions packages/sitecore-jss/src/i18n/graphql-dictionary-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ||
Expand Down