Skip to content

Commit a75a453

Browse files
[Next.js][Multi-site] Multi-site path utils (#1275)
* [Next.js][Multi-site] Multi-site path utils * Add return type to `getSiteRewriteData` Co-authored-by: Adam Brauer <[email protected]>
1 parent 9d42e01 commit a75a453

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

packages/sitecore-jss/src/site/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ export {
2525
GraphQLErrorPagesServiceConfig,
2626
} from './graphql-error-pages-service';
2727

28+
export { getSiteRewrite, getSiteRewriteData, normalizeSiteRewrite, SiteRewriteData } from './utils';
2829
export { SiteResolver, HostInfo } from './site-resolver';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { expect } from 'chai';
2+
import { getSiteRewrite, getSiteRewriteData, normalizeSiteRewrite, SITE_PREFIX } from './utils';
3+
4+
describe('utils', () => {
5+
describe('getSiteRewrite', () => {
6+
const data = {
7+
siteName: 'jss',
8+
};
9+
10+
it('should return a string', () => {
11+
expect(getSiteRewrite('/pathname', data)).to.be.a('string');
12+
});
13+
14+
it('should return the path with the site name when pathname starts with "/"', () => {
15+
const pathname = '/some/path';
16+
const result = getSiteRewrite(pathname, data);
17+
expect(result).to.equal(`/${SITE_PREFIX}${data.siteName}/some/path`);
18+
});
19+
20+
it('should return the path with the site name when pathname not starts with "/"', () => {
21+
const pathname = 'some/path';
22+
const result = getSiteRewrite(pathname, data);
23+
expect(result).to.equal(`/${SITE_PREFIX}${data.siteName}/some/path`);
24+
});
25+
26+
it('should return the root path with the site name', () => {
27+
const pathname = '/';
28+
const result = getSiteRewrite(pathname, data);
29+
expect(result).to.equal(`/${SITE_PREFIX}${data.siteName}/`);
30+
});
31+
});
32+
33+
describe('getSiteRewriteData', () => {
34+
const defaultSiteName = 'foo';
35+
36+
it('should return a MultiSiteRewriteData object', () => {
37+
expect(getSiteRewriteData('/some/path', defaultSiteName)).to.be.an('object');
38+
});
39+
40+
it('should return the multisite data from the rewrite path', () => {
41+
const pathname = `/some/path/${SITE_PREFIX}bar/`;
42+
const result = getSiteRewriteData(pathname, defaultSiteName);
43+
expect(result.siteName).to.equal('bar');
44+
});
45+
46+
it('should return the default site name when pathname does not contain site name', () => {
47+
const pathname = '/some/path';
48+
const result = getSiteRewriteData(pathname, defaultSiteName);
49+
expect(result.siteName).to.equal(defaultSiteName);
50+
});
51+
52+
it('should return empty site name when pathname is missing site name', () => {
53+
const pathname = `/some/path/${SITE_PREFIX}/`;
54+
const result = getSiteRewriteData(pathname, defaultSiteName);
55+
expect(result.siteName).to.equal(defaultSiteName);
56+
});
57+
});
58+
59+
describe('normalizeSiteRewrite', () => {
60+
it('should return a string', () => {
61+
expect(normalizeSiteRewrite('/some/path')).to.be.a('string');
62+
});
63+
64+
it('should return the pathname when it does not contain site prefix', () => {
65+
const pathname = '/some/path';
66+
const result = normalizeSiteRewrite(pathname);
67+
expect(result).to.equal(pathname);
68+
});
69+
70+
it('should return the pathname without the site name', () => {
71+
const pathname = `/${SITE_PREFIX}foo/some/path`;
72+
const result = normalizeSiteRewrite(pathname);
73+
expect(result).to.equal('/some/path');
74+
});
75+
76+
it('should return the root pathname without the site name', () => {
77+
const pathname = `/${SITE_PREFIX}foo/`;
78+
const result = normalizeSiteRewrite(pathname);
79+
expect(result).to.equal('/');
80+
});
81+
82+
it('should return the root pathname without the site name when pathname not ends with "/"', () => {
83+
const pathname = `/${SITE_PREFIX}foo`;
84+
const result = normalizeSiteRewrite(pathname);
85+
expect(result).to.equal('/');
86+
});
87+
});
88+
});
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
export const SITE_PREFIX = '_site_';
2+
3+
export type SiteRewriteData = {
4+
siteName: string;
5+
};
6+
7+
/**
8+
* Get a site rewrite path for given pathname
9+
* @param {string} pathname the pathname
10+
* @param {SiteRewriteData} data the site data to include in the rewrite
11+
* @returns {string} the rewrite path
12+
*/
13+
export function getSiteRewrite(pathname: string, data: SiteRewriteData): string {
14+
const path = pathname.startsWith('/') ? pathname : '/' + pathname;
15+
16+
return `/${SITE_PREFIX}${data.siteName}${path}`;
17+
}
18+
19+
/**
20+
* Get site data from the rewrite path
21+
* @param {string} pathname the pathname
22+
* @param {string} defaultSiteName the default site name
23+
* @returns {SiteRewriteData} the site data from the rewrite
24+
*/
25+
export function getSiteRewriteData(pathname: string, defaultSiteName: string): SiteRewriteData {
26+
const data: SiteRewriteData = {
27+
siteName: defaultSiteName,
28+
};
29+
30+
const path = pathname.endsWith('/') ? pathname : pathname + '/';
31+
const result = path.match(`${SITE_PREFIX}(.*?)\\/`);
32+
33+
if (result && result[1] !== '') {
34+
data.siteName = result[1];
35+
}
36+
37+
return data;
38+
}
39+
40+
/**
41+
* Normalize a site rewrite path (remove site data)
42+
* @param {string} pathname the pathname
43+
* @returns {string} the pathname with site data removed
44+
*/
45+
export function normalizeSiteRewrite(pathname: string): string {
46+
const result = pathname.match(`${SITE_PREFIX}.*?(?:\\/|$)`);
47+
48+
return result === null ? pathname : pathname.replace(result[0], '');
49+
}

0 commit comments

Comments
 (0)