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 #1224

Merged
merged 3 commits into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
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 } from './site-resolver';
37 changes: 37 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,37 @@
import { expect } from 'chai';
import { SiteResolver } from './site-resolver';

describe('SiteResolver', () => {
const sitesInfo = [
{
name: 'cat',
hostName: 'supercat.com',
virtualFolder: '/north',
language: 'en',
},
{
name: 'dog',
hostName: 'superdog.com',
virtualFolder: '/south',
language: 'da-DK',
},
];

it('should resolve site name', () => {
const siteName = SiteResolver.resolve('superdog.com', sitesInfo);

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

it('should return custom fallback site name', () => {
const siteName = SiteResolver.resolve('superanimal.com', sitesInfo, 'animal');

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

it('should return default fallback site name', () => {
const siteName = SiteResolver.resolve('superanimal.com', sitesInfo);

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

// This resolver is used to determine site name based on the provided hostname
export class SiteResolver {
/**
* Resolve siteName by hostName
* @param {string} hostName
* @param {SiteInfo[]} sitesInfo
* @param {string} [fallbackSiteName] siteName to be returned in case siteName is not found
* @returns {string} siteName
*/
static resolve = (hostName: string, sitesInfo: SiteInfo[], fallbackSiteName?: string): string => {
const siteInfo = sitesInfo.find((info) => info.hostName === hostName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hostname can have a wildcard ("*") value, we need to account for that

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean sitesInfo.hostname

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how it should be resolved in this case? which sitename should be returned, or fallback should be provided?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to clarify what is expected and supported (with extended teams) before we go further here. Specifically, regarding special tokens (wildcard, other REGEX chars?) and multi-value (e.g. "host1.com|host2.com").


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