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] Multi-site path utils #1275

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

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

describe('utils', () => {
describe('getSiteRewrite', () => {
const data = {
siteName: 'jss',
};

it('should return a string', () => {
expect(getSiteRewrite('/pathname', data)).to.be.a('string');
});

it('should return the path with the site name when pathname starts with "/"', () => {
const pathname = '/some/path';
const result = getSiteRewrite(pathname, data);
expect(result).to.equal(`/${SITE_PREFIX}${data.siteName}/some/path`);
});

it('should return the path with the site name when pathname not starts with "/"', () => {
const pathname = 'some/path';
const result = getSiteRewrite(pathname, data);
expect(result).to.equal(`/${SITE_PREFIX}${data.siteName}/some/path`);
});

it('should return the root path with the site name', () => {
const pathname = '/';
const result = getSiteRewrite(pathname, data);
expect(result).to.equal(`/${SITE_PREFIX}${data.siteName}/`);
});
});

describe('getSiteRewriteData', () => {
const defaultSiteName = 'foo';

it('should return a MultiSiteRewriteData object', () => {
expect(getSiteRewriteData('/some/path', defaultSiteName)).to.be.an('object');
});

it('should return the multisite data from the rewrite path', () => {
const pathname = `/some/path/${SITE_PREFIX}bar/`;
const result = getSiteRewriteData(pathname, defaultSiteName);
expect(result.siteName).to.equal('bar');
});

it('should return the default site name when pathname does not contain site name', () => {
const pathname = '/some/path';
const result = getSiteRewriteData(pathname, defaultSiteName);
expect(result.siteName).to.equal(defaultSiteName);
});

it('should return empty site name when pathname is missing site name', () => {
const pathname = `/some/path/${SITE_PREFIX}/`;
const result = getSiteRewriteData(pathname, defaultSiteName);
expect(result.siteName).to.equal(defaultSiteName);
});
});

describe('normalizeSiteRewrite', () => {
it('should return a string', () => {
expect(normalizeSiteRewrite('/some/path')).to.be.a('string');
});

it('should return the pathname when it does not contain site prefix', () => {
const pathname = '/some/path';
const result = normalizeSiteRewrite(pathname);
expect(result).to.equal(pathname);
});

it('should return the pathname without the site name', () => {
const pathname = `/${SITE_PREFIX}foo/some/path`;
const result = normalizeSiteRewrite(pathname);
expect(result).to.equal('/some/path');
});

it('should return the root pathname without the site name', () => {
const pathname = `/${SITE_PREFIX}foo/`;
const result = normalizeSiteRewrite(pathname);
expect(result).to.equal('/');
});

it('should return the root pathname without the site name when pathname not ends with "/"', () => {
const pathname = `/${SITE_PREFIX}foo`;
const result = normalizeSiteRewrite(pathname);
expect(result).to.equal('/');
});
});
});
49 changes: 49 additions & 0 deletions packages/sitecore-jss/src/site/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
export const SITE_PREFIX = '_site_';

export type SiteRewriteData = {
siteName: string;
};

/**
* Get a site rewrite path for given pathname
* @param {string} pathname the pathname
* @param {SiteRewriteData} data the site data to include in the rewrite
* @returns {string} the rewrite path
*/
export function getSiteRewrite(pathname: string, data: SiteRewriteData): string {
const path = pathname.startsWith('/') ? pathname : '/' + pathname;

return `/${SITE_PREFIX}${data.siteName}${path}`;
}

/**
* Get site data from the rewrite path
* @param {string} pathname the pathname
* @param {string} defaultSiteName the default site name
* @returns {SiteRewriteData} the site data from the rewrite
*/
export function getSiteRewriteData(pathname: string, defaultSiteName: string): SiteRewriteData {
const data: SiteRewriteData = {
siteName: defaultSiteName,
};

const path = pathname.endsWith('/') ? pathname : pathname + '/';
const result = path.match(`${SITE_PREFIX}(.*?)\\/`);

if (result && result[1] !== '') {
data.siteName = result[1];
}

return data;
}

/**
* Normalize a site rewrite path (remove site data)
* @param {string} pathname the pathname
* @returns {string} the pathname with site data removed
*/
export function normalizeSiteRewrite(pathname: string): string {
const result = pathname.match(`${SITE_PREFIX}.*?(?:\\/|$)`);

return result === null ? pathname : pathname.replace(result[0], '');
}