-
Notifications
You must be signed in to change notification settings - Fork 279
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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 <400763+ambrauer@users.noreply.github.com>
- Loading branch information
1 parent
9d42e01
commit a75a453
Showing
3 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('/'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], ''); | ||
} |