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

[Next.js][Multi-site] Dynamic site resolver #1271

Merged
merged 5 commits into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions packages/sitecore-jss/src/site/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ export {
GraphQLErrorPagesService,
GraphQLErrorPagesServiceConfig,
} from './graphql-error-pages-service';

export { SiteResolver, HostInfo } from './site-resolver';
103 changes: 103 additions & 0 deletions packages/sitecore-jss/src/site/site-resolver.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { expect } from 'chai';
import { HostInfo, SiteResolver } from './site-resolver';

describe('SiteResolver', () => {
const hostInfo: HostInfo = {
hostName: 'foo.com',
};

const hostInfoWithLanguage: HostInfo = {
...hostInfo,
language: 'en',
};

describe('resolve', () => {
describe('fallback site name', () => {
it('should return default when sites info is empty', () => {
const siteName = SiteResolver.resolve(hostInfo, []);

expect(siteName).to.equal('website');
});

it('should return default when there is no appropriate site info', () => {
const siteName = SiteResolver.resolve(hostInfo, [
{ hostName: 'bar.com', language: '', name: 'bar' },
{ hostName: 'var.com', language: '', name: 'var' },
]);

expect(siteName).to.equal('website');
});

it('should return custom when sites info is empty', () => {
const siteName = SiteResolver.resolve(hostInfo, [], 'sample');

expect(siteName).to.equal('sample');
});

it('should return custom when there is no appropriate site info', () => {
const siteName = SiteResolver.resolve(
hostInfo,
[
{ hostName: 'bar.com', language: '', name: 'bar' },
{ hostName: 'var.com', language: '', name: 'var' },
],
'sample'
);

expect(siteName).to.equal('sample');
});
});

it('should return site name when only hostname is provided', () => {
const siteName = SiteResolver.resolve(hostInfo, [
{ hostName: 'bar.net', language: '', name: 'bar' },
{ hostName: 'foo.com', language: '', name: 'foo' },
{ hostName: 'var.com', language: '', name: 'var' },
]);

expect(siteName).to.equal('foo');
});

it('should return site name when wildcard is provided', () => {
const siteName = SiteResolver.resolve(hostInfo, [
{ hostName: 'bar.net', language: '', name: 'bar' },
{ hostName: '*', language: '', name: 'wildcard' },
{ hostName: 'foo.com', language: '', name: 'foo' },
]);

expect(siteName).to.equal('wildcard');
});

it('should return site name when language is provided', () => {
const siteName = SiteResolver.resolve(hostInfoWithLanguage, [
{ hostName: 'foo.com', language: 'ca', name: 'foo-ca' },
{ hostName: 'var.com', language: 'da-DK', name: 'var' },
{ hostName: 'bar.net', language: 'en', name: 'bar' },
{ hostName: 'foo.com', language: 'en', name: 'foo-en' },
]);

expect(siteName).to.equal('foo-en');
});

it('should return site name when language is omit', () => {
const siteName = SiteResolver.resolve(hostInfoWithLanguage, [
{ hostName: 'var.com', language: 'da-DK', name: 'var' },
{ hostName: 'bar.net', language: 'en', name: 'bar' },
{ hostName: 'foo.com', language: '', name: 'foo' },
{ hostName: 'foo.com', language: 'en', name: 'foo-en' },
]);

expect(siteName).to.equal('foo');
});

it('should return site name when multi-value hostnames are provided', () => {
const siteName = SiteResolver.resolve(hostInfoWithLanguage, [
{ hostName: 'bar.net', language: '', name: 'bar' },
{ hostName: 'test.com|foo.net|foo.com', language: 'en', name: 'foo' },
{ hostName: 'var.com', language: '', name: 'var' },
]);

expect(siteName).to.equal('foo');
});
});
});
46 changes: 46 additions & 0 deletions packages/sitecore-jss/src/site/site-resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { SiteInfo } from './graphql-siteinfo-service';

/**
* Information about the current host
*/
export type HostInfo = {
hostName: string;
language?: string;
};

// Delimiters for multi-value hostnames
const DELIMITERS = /\|/g;
illiakovalenko marked this conversation as resolved.
Show resolved Hide resolved

// Wildcard hostname
const WILDCARD = '*';

/**
* Determines site name based on the provided hostname
illiakovalenko marked this conversation as resolved.
Show resolved Hide resolved
*/
export class SiteResolver {
/**
* Resolve siteName by hostName
illiakovalenko marked this conversation as resolved.
Show resolved Hide resolved
* @param {HostInfo} hostInfo
* @param {SiteInfo[]} sitesInfo
illiakovalenko marked this conversation as resolved.
Show resolved Hide resolved
* @param {string} [fallbackSiteName] siteName to be returned in case siteName is not found
* @returns {string} siteName
*/
static resolve = (
hostInfo: HostInfo,
sitesInfo: SiteInfo[],
fallbackSiteName = 'website'
): string => {
const siteInfo = sitesInfo.find((info) => {
const hostnames = info.hostName.split(DELIMITERS);
illiakovalenko marked this conversation as resolved.
Show resolved Hide resolved

const languageMatches =
info.language === '' || !hostInfo.language || hostInfo.language === info.language;

return hostnames.some(
(value) => languageMatches && (hostInfo.hostName === value || value === WILDCARD)
illiakovalenko marked this conversation as resolved.
Show resolved Hide resolved
);
});

return siteInfo?.name || fallbackSiteName;
};
}