Skip to content

Commit

Permalink
[sitecore-jss] Switch to edge site query for XP (#1772)
Browse files Browse the repository at this point in the history
* switch to use site query for SXP

* update changelog

* update upgrade guide

* update upgrade guide
  • Loading branch information
addy-pathania authored Apr 16, 2024
1 parent f2d95d3 commit 54f01a5
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 308 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ Our versioning strategy is as follows:

## Unreleased

### 🛠 Breaking Changes

[sitecore-jss] Switch to edge site query for XP and gets config sites + sxa sites (ignoring website)
* Previously introduced Boolean `useSiteQuery` switch for XMCloud users has been removed.
* Search query usage has been removed.
* If you have any non-nextjs sites they should filter them out in multisite config plugin

## 21.7.1

### 🐛 Bug Fixes
Expand Down
7 changes: 7 additions & 0 deletions docs/upgrades/unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@

### nextjs-multisite

* **Update** packages\create-sitecore-jss\src\templates\nextjs-multisite\scripts\config\plugins\multisite.ts

```
//Remove
useSiteQuery: true,
```

### nextjs-styleguide

### react
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ class MultisitePlugin implements ConfigPlugin {
try {
const siteInfoService = new GraphQLSiteInfoService({
clientFactory: createGraphQLClientFactory(config),
<% if (templates.includes("nextjs-xmcloud")) { -%>
// enable site query for the service. Only works on XMCloud currently
useSiteQuery: true,
<% } -%>
});
sites = await siteInfoService.fetchSiteInfo();
} catch (error) {
Expand Down
236 changes: 30 additions & 206 deletions packages/sitecore-jss/src/site/graphql-siteinfo-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@
import { expect, spy, use } from 'chai';
import spies from 'chai-spies';
import nock from 'nock';
import {
GraphQLSiteInfoService,
GraphQLSiteInfoResult,
GraphQLXmCloudSiteInfoResult,
} from './graphql-siteinfo-service';
import { GraphQLRequestClient, PageInfo } from '../graphql';
import { GraphQLSiteInfoService, GraphQLSiteInfoResult } from './graphql-siteinfo-service';
import { GraphQLRequestClient } from '../graphql';
import debugApi from 'debug';
import debug from '../debug';

Expand All @@ -28,29 +24,24 @@ describe('GraphQLSiteInfoService', () => {
hostName: string;
language: string;
}): GraphQLSiteInfoResult => ({
name: { value: name },
hostName: { value: hostName },
language: { value: language },
name: name,
hostName: hostName,
language: language,
});

const nonEmptyResponse = ({
count = 1,
start = 0,
pageInfo = { hasNext: false, endCursor: '' },
sites = [],
}: {
count?: number;
start?: number;
pageInfo?: PageInfo;
sites?: GraphQLSiteInfoResult[];
} = {}) => ({
data: {
search: {
pageInfo,
results: [
site: {
siteInfoCollection: [
...[...Array(count).keys()].map((n) =>
site({
name: `site ${start + n}`,
name: `site ${n}`,
hostName: 'restricted.gov',
language: 'en',
})
Expand All @@ -63,9 +54,8 @@ describe('GraphQLSiteInfoService', () => {

const emptyResponse = {
data: {
search: {
pageInfo: {},
results: [],
site: {
siteInfoCollection: [],
},
},
};
Expand Down Expand Up @@ -154,52 +144,6 @@ describe('GraphQLSiteInfoService', () => {
},
]);
});

it('should return correct result using custom pageSize', async () => {
mockSiteInfoRequest(nonEmptyResponse({ count: 2, pageInfo: { hasNext: true, endCursor: '' } }));
mockSiteInfoRequest(
nonEmptyResponse({ count: 2, start: 2, pageInfo: { hasNext: true, endCursor: '' } })
);
mockSiteInfoRequest(
nonEmptyResponse({ count: 2, start: 4, pageInfo: { hasNext: false, endCursor: '' } })
);

const service = new GraphQLSiteInfoService({ apiKey: apiKey, endpoint: endpoint, pageSize: 2 });
const result = await service.fetchSiteInfo();
expect(result).to.be.deep.equal([
{
name: 'site 0',
hostName: 'restricted.gov',
language: 'en',
},
{
name: 'site 1',
hostName: 'restricted.gov',
language: 'en',
},
{
name: 'site 2',
hostName: 'restricted.gov',
language: 'en',
},
{
name: 'site 3',
hostName: 'restricted.gov',
language: 'en',
},
{
name: 'site 4',
hostName: 'restricted.gov',
language: 'en',
},
{
name: 'site 5',
hostName: 'restricted.gov',
language: 'en',
},
]);
});

it('should return empty array when empty result received', async () => {
nock(endpoint)
.post('/')
Expand Down Expand Up @@ -271,146 +215,26 @@ describe('GraphQLSiteInfoService', () => {
expect(nock.isDone(), 'skip request').to.be.false;
});

describe('Fetch with site query in XM Cloud', () => {
const site = ({
name,
hostName,
language,
}: {
name: string;
hostName: string;
language: string;
}): GraphQLXmCloudSiteInfoResult => ({
name,
hostName,
language,
});

const nonEmptyResponse = ({
count = 1,
sites = [],
}: {
count?: number;
sites?: GraphQLXmCloudSiteInfoResult[];
} = {}) => ({
data: {
site: {
siteInfoCollection: [
...[...Array(count).keys()].map((n) =>
site({
name: `site ${n}`,
hostName: 'restricted.gov',
language: 'en',
})
),
...sites,
],
},
},
});

const emptyResponse = {
data: {
site: {
siteInfoCollection: [],
},
it('should filter out default website', async () => {
mockSiteInfoRequest(
nonEmptyResponse({
sites: [
site({
name: 'website',
hostName: 'notheadless.org',
language: '',
}),
],
})
);
const service = new GraphQLSiteInfoService({ apiKey: apiKey, endpoint: endpoint });
const result = await service.fetchSiteInfo();
expect(result).to.be.deep.equal([
{
name: 'site 0',
hostName: 'restricted.gov',
language: 'en',
},
};

const getSiteQuerySiteInfoService = (initProps: { [key: string]: unknown }) => {
return new GraphQLSiteInfoService({ useSiteQuery: true, ...initProps });
};

it('should return correct result', async () => {
mockSiteInfoRequest(
nonEmptyResponse({
sites: [
site({
name: 'public 0',
hostName: 'pr.showercurtains.org',
language: '',
}),
],
})
);
const service = getSiteQuerySiteInfoService({ apiKey: apiKey, endpoint: endpoint });
const result = await service.fetchSiteInfo();
expect(result).to.be.deep.equal([
{
name: 'site 0',
hostName: 'restricted.gov',
language: 'en',
},
{
name: 'public 0',
hostName: 'pr.showercurtains.org',
language: '',
},
]);
});

it('should return correct result using clientFactory', async () => {
mockSiteInfoRequest(
nonEmptyResponse({
sites: [
site({
name: 'public 0',
hostName: 'pr.showercurtains.org',
language: '',
}),
],
})
);
const clientFactory = GraphQLRequestClient.createClientFactory({
endpoint,
apiKey,
});
const service = getSiteQuerySiteInfoService({ clientFactory });
const result = await service.fetchSiteInfo();
expect(result).to.be.deep.equal([
{
name: 'site 0',
hostName: 'restricted.gov',
language: 'en',
},
{
name: 'public 0',
hostName: 'pr.showercurtains.org',
language: '',
},
]);
});

it('should return empty array when empty result received', async () => {
nock(endpoint)
.post('/')
.reply(200, emptyResponse);
const service = getSiteQuerySiteInfoService({ apiKey: apiKey, endpoint: endpoint });
const result = await service.fetchSiteInfo();
expect(result).to.deep.equal([]);
});

it('should filter out default website', async () => {
mockSiteInfoRequest(
nonEmptyResponse({
sites: [
site({
name: 'website',
hostName: 'notheadless.org',
language: '',
}),
],
})
);
const service = getSiteQuerySiteInfoService({ apiKey: apiKey, endpoint: endpoint });
const result = await service.fetchSiteInfo();
expect(result).to.be.deep.equal([
{
name: 'site 0',
hostName: 'restricted.gov',
language: 'en',
},
]);
});
]);
});
});
Loading

0 comments on commit 54f01a5

Please sign in to comment.