Skip to content

Commit

Permalink
Moved skipping of site information fetch on XM Cloud to base package …
Browse files Browse the repository at this point in the history
…(GraphQLSiteInfoService)
  • Loading branch information
ambrauer committed Nov 6, 2023
1 parent 2df0a00 commit 1e8f7b4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
36 changes: 35 additions & 1 deletion packages/sitecore-jss/src/site/graphql-siteinfo-service.test.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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 }) => {
Expand Down Expand Up @@ -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;
});
});
4 changes: 4 additions & 0 deletions packages/sitecore-jss/src/site/graphql-siteinfo-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 1e8f7b4

Please sign in to comment.