diff --git a/packages/create-sitecore-jss/src/templates/nextjs-multisite/scripts/config/plugins/multisite.ts b/packages/create-sitecore-jss/src/templates/nextjs-multisite/scripts/config/plugins/multisite.ts index dce37363b5..56f7581f1c 100644 --- a/packages/create-sitecore-jss/src/templates/nextjs-multisite/scripts/config/plugins/multisite.ts +++ b/packages/create-sitecore-jss/src/templates/nextjs-multisite/scripts/config/plugins/multisite.ts @@ -14,20 +14,15 @@ class MultisitePlugin implements ConfigPlugin { async exec(config: JssConfig) { let sites: SiteInfo[] = []; - - if (process.env.SITECORE) { - console.warn(chalk.yellow('Skipping site information fetch (building on XM Cloud)')); - } else { - console.log('Fetching site information'); - try { - const siteInfoService = new GraphQLSiteInfoService({ - clientFactory: createGraphQLClientFactory(config), - }); - sites = await siteInfoService.fetchSiteInfo(); - } catch (error) { - console.error(chalk.red('Error fetching site information')); - console.error(error); - } + console.log('Fetching site information'); + try { + const siteInfoService = new GraphQLSiteInfoService({ + clientFactory: createGraphQLClientFactory(config), + }); + sites = await siteInfoService.fetchSiteInfo(); + } catch (error) { + console.error(chalk.red('Error fetching site information')); + console.error(error); } return Object.assign({}, config, { diff --git a/packages/sitecore-jss/src/site/graphql-siteinfo-service.test.ts b/packages/sitecore-jss/src/site/graphql-siteinfo-service.test.ts index d9c95db7d4..53f61b98a5 100644 --- a/packages/sitecore-jss/src/site/graphql-siteinfo-service.test.ts +++ b/packages/sitecore-jss/src/site/graphql-siteinfo-service.test.ts @@ -1,10 +1,17 @@ +/* eslint-disable no-unused-expressions */ /* eslint-disable @typescript-eslint/no-unused-vars */ -import { expect } from 'chai'; +import { expect, spy, use } from 'chai'; +import spies from 'chai-spies'; import nock from 'nock'; import { GraphQLSiteInfoService, GraphQLSiteInfoResult } from './graphql-siteinfo-service'; import { GraphQLRequestClient, PageInfo } from '../graphql'; +import debugApi from 'debug'; +import debug from '../debug'; + +use(spies); describe('GraphQLSiteInfoService', () => { + let debugNamespaces: string; const endpoint = 'http://site'; const apiKey = 'some-api-key'; @@ -63,8 +70,23 @@ describe('GraphQLSiteInfoService', () => { }, }; + before(() => { + debugNamespaces = debugApi.disable(); + debugApi.enable(debug.multisite.namespace); + }); + + beforeEach(() => { + spy.on(debug.multisite, 'log', () => true); + }); + afterEach(() => { nock.cleanAll(); + spy.restore(debug.multisite); + delete process.env.SITECORE; + }); + + after(() => { + debugApi.enable(debugNamespaces); }); const mockSiteInfoRequest = (response: { [key: string]: unknown }) => { @@ -269,4 +291,16 @@ describe('GraphQLSiteInfoService', () => { const resultCached = await service.fetchSiteInfo(); expect(resultCached).to.deep.equal([]); }); + + it('should skip on XM Cloud', async () => { + process.env.SITECORE = 'true'; + nock(endpoint) + .post('/') + .reply(200, emptyResponse); + const service = new GraphQLSiteInfoService({ apiKey: apiKey, endpoint: endpoint }); + const result = await service.fetchSiteInfo(); + expect(result).to.deep.equal([]); + expect(debug.multisite.log, 'log debug message').to.be.called.once; + expect(nock.isDone(), 'skip request').to.be.false; + }); }); diff --git a/packages/sitecore-jss/src/site/graphql-siteinfo-service.ts b/packages/sitecore-jss/src/site/graphql-siteinfo-service.ts index 8c57ca256c..c0b87758f4 100644 --- a/packages/sitecore-jss/src/site/graphql-siteinfo-service.ts +++ b/packages/sitecore-jss/src/site/graphql-siteinfo-service.ts @@ -134,6 +134,10 @@ export class GraphQLSiteInfoService { if (cachedResult) { return cachedResult; } + if (process.env.SITECORE) { + debug.multisite('Skipping site information fetch (building on XM Cloud)'); + return []; + } const results: SiteInfo[] = []; let hasNext = true;