Skip to content

Commit

Permalink
Merge 279130b into dab722e
Browse files Browse the repository at this point in the history
  • Loading branch information
tmeyer2115 committed May 24, 2021
2 parents dab722e + 279130b commit 5e32278
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 46 deletions.
35 changes: 35 additions & 0 deletions hbshelpers/sdkAssetUrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const RELEASE_BRANCH_REGEX = /^release\/v[0-9.]+$/;
const HOTFIX_BRANCH_REGEX = /^hotfix\/v[0-9.]+$/;
const SEM_VER_REGEX = /^[1-9]+$|^[1-9]+\.[0-9]+$|^[1-9]+\.[0-9]+\.[0-9]+$/;

/**
* Given a branch (or release) of the SDK and a locale, this helper provides the correct
* URL in the CDN for the asset.
*
* @param {string} branch The branch (or release) of the SDK.
* @param {string} locale The locale to use.
* @param {string} assetName The name of the desired asset.
* @returns {string} The CDN URL of the localized asset.
*/
module.exports = function sdkAssetUrl(branch, locale, assetName) {
const isReleasedBranch = SEM_VER_REGEX.test(branch);

let parsedBranch;
if (isReleasedBranch) {
parsedBranch = `v${branch}`;
} else {
parsedBranch = `dev/${branch.replace(/\//g, '-')}`;
}

const isPreReleaseBranch =
RELEASE_BRANCH_REGEX.test(branch) || HOTFIX_BRANCH_REGEX.test(branch);
const isLocalizationSupported =
(isReleasedBranch || isPreReleaseBranch) &&
!(locale.startsWith('en') || assetName === 'answers.css') ;

const parsedAssetName = isLocalizationSupported ?
`${locale}-${assetName}` :
assetName;

return `https://assets.sitescdn.net/answers/${parsedBranch}/${parsedAssetName}`;
};
2 changes: 1 addition & 1 deletion layouts/html.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
{{/babel}}
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/iframe-resizer/4.2.10/iframeResizer.contentWindow.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://assets.sitescdn.net/answers/v{{global_config.sdkVersion}}/answers.css">
<link rel="stylesheet" type="text/css" href="{{sdkAssetUrl global_config.sdkVersion 'en' 'answers.css'}}">
<link rel="stylesheet" type="text/css" href="{{relativePath}}/bundle.css" data-webpack-inline>
</head>
<body>
Expand Down
2 changes: 1 addition & 1 deletion script/core.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
console.error('ERROR: no sdkVersion specified, please specify an sdkVersion in the global_config.');
}
</script>
<script src="https://assets.sitescdn.net/answers/v{{global_config.sdkVersion}}/{{#if params.sdkLocaleOverride}}{{#unless (eq params.sdkLocaleOverride 'en')}}{{params.sdkLocaleOverride}}-{{/unless}}{{else if global_config.locale}}{{#unless (eq global_config.locale 'en')}}{{global_config.locale}}-{{/unless}}{{/if}}answerstemplates.compiled.min.js" defer></script>
<script src="{{sdkAssetUrl global_config.sdkVersion (findFirst params.sdkLocaleOverride global_config.locale 'en') 'answerstemplates.compiled.min.js'}}" defer></script>
<script>
{{#babel}}
function initAnswers() {
Expand Down
4 changes: 2 additions & 2 deletions script/partials/sdk-js-script-tags.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
@param sdkVersion
--}}
<script
src="{{> script/partials/sdk-url sdkAsset="answers-modern.min.js" locale=locale sdkVersion=sdkVersion}}"
src="{{sdkAssetUrl sdkVersion locale "answers-modern.min.js"}}"
type="module"
onload="initAnswers()"
></script>
<script
src="{{> script/partials/sdk-url sdkAsset="answers.min.js" locale=locale sdkVersion=sdkVersion}}"
src="{{sdkAssetUrl sdkVersion locale "answers.min.js"}}"
nomodule
onload="initAnswers()"
defer
Expand Down
15 changes: 0 additions & 15 deletions script/partials/sdk-url.hbs

This file was deleted.

73 changes: 73 additions & 0 deletions tests/hbshelpers/sdkAssetUrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import sdkAssetUrl from '../../hbshelpers/sdkAssetUrl';

describe('URLs are computed properly for released versions', () => {
it('works correctly when locale is "en"', () => {
const expectedJSUrl = 'https://assets.sitescdn.net/answers/v1.8/answers.min.js';
const expectedCSSUrl = 'https://assets.sitescdn.net/answers/v1.8/answers.css'

expect(sdkAssetUrl('1.8', 'en', 'answers.min.js')).toEqual(expectedJSUrl);
expect(sdkAssetUrl('1.8', 'en', 'answers.css')).toEqual(expectedCSSUrl);
});

it('works correctly when locale is not "en"', () => {
const expectedJSUrl = 'https://assets.sitescdn.net/answers/v1.8/fr-answers.min.js';
const expectedCSSUrl = 'https://assets.sitescdn.net/answers/v1.8/answers.css'

expect(sdkAssetUrl('1.8', 'fr', 'answers.min.js')).toEqual(expectedJSUrl);
expect(sdkAssetUrl('1.8', 'fr', 'answers.css')).toEqual(expectedCSSUrl);
});
});

describe('URLs are computed properly for release branches', () => {
it('works correctly when locale is "en"', () => {
const expectedJSUrl = 'https://assets.sitescdn.net/answers/dev/release-v1.8/answers.min.js';
const expectedCSSUrl = 'https://assets.sitescdn.net/answers/dev/release-v1.8/answers.css'

expect(sdkAssetUrl('release/v1.8', 'en', 'answers.min.js')).toEqual(expectedJSUrl);
expect(sdkAssetUrl('release/v1.8', 'en', 'answers.css')).toEqual(expectedCSSUrl);
});

it('works correctly when locale is not "en"', () => {
const expectedJSUrl = 'https://assets.sitescdn.net/answers/dev/release-v1.8/fr-answers.min.js';
const expectedCSSUrl = 'https://assets.sitescdn.net/answers/dev/release-v1.8/answers.css'

expect(sdkAssetUrl('release/v1.8', 'fr', 'answers.min.js')).toEqual(expectedJSUrl);
expect(sdkAssetUrl('release/v1.8', 'fr', 'answers.css')).toEqual(expectedCSSUrl);
});
});

describe('URLs are computed properly for hotfix branches', () => {
it('works correctly when locale is "en"', () => {
const expectedJSUrl = 'https://assets.sitescdn.net/answers/dev/hotfix-v1.8.1/answers.min.js';
const expectedCSSUrl = 'https://assets.sitescdn.net/answers/dev/hotfix-v1.8.1/answers.css'

expect(sdkAssetUrl('hotfix/v1.8.1', 'en', 'answers.min.js')).toEqual(expectedJSUrl);
expect(sdkAssetUrl('hotfix/v1.8.1', 'en', 'answers.css')).toEqual(expectedCSSUrl);
});

it('works correctly when locale is not "en"', () => {
const expectedJSUrl = 'https://assets.sitescdn.net/answers/dev/hotfix-v1.8.1/fr-answers.min.js';
const expectedCSSUrl = 'https://assets.sitescdn.net/answers/dev/hotfix-v1.8.1/answers.css'

expect(sdkAssetUrl('hotfix/v1.8.1', 'fr', 'answers.min.js')).toEqual(expectedJSUrl);
expect(sdkAssetUrl('hotfix/v1.8.1', 'fr', 'answers.css')).toEqual(expectedCSSUrl);
});
});

describe('URLs are computed properly for all other branches', () => {
it('works correctly when locale is "en"', () => {
const expectedJSUrl = 'https://assets.sitescdn.net/answers/dev/feature-foo/answers.min.js';
const expectedCSSUrl = 'https://assets.sitescdn.net/answers/dev/feature-foo/answers.css'

expect(sdkAssetUrl('feature/foo', 'en', 'answers.min.js')).toEqual(expectedJSUrl);
expect(sdkAssetUrl('feature/foo', 'en', 'answers.css')).toEqual(expectedCSSUrl);
});

it('works correctly when locale is not "en"', () => {
const expectedJSUrl = 'https://assets.sitescdn.net/answers/dev/feature-foo/answers.min.js';
const expectedCSSUrl = 'https://assets.sitescdn.net/answers/dev/feature-foo/answers.css'

expect(sdkAssetUrl('feature/foo', 'fr', 'answers.min.js')).toEqual(expectedJSUrl);
expect(sdkAssetUrl('feature/foo', 'fr', 'answers.css')).toEqual(expectedCSSUrl);
});
});
5 changes: 0 additions & 5 deletions tests/script/partials/sdk-js-script-tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ const jaOutput = `<script
defer
></script>`;

it('creates correct tags when no locale specified', () => {
const partialUsage = '{{> script/partials/sdk-js-script-tags sdkVersion=1.8}}'
expect(hbs.compile(partialUsage)()).toEqual(defaultOutput);
});

it('creates correct tags for en', () => {
const partialUsage = `{{> script/partials/sdk-js-script-tags
sdkVersion=1.8
Expand Down
22 changes: 0 additions & 22 deletions tests/script/partials/sdk-url.js

This file was deleted.

0 comments on commit 5e32278

Please sign in to comment.