Skip to content

Commit

Permalink
Fix dynamic extension path (ampproject#19340)
Browse files Browse the repository at this point in the history
* handle esm case

* fix how we dynamically fetch scripts for single pass experiment

* fix tests

* remove erroneous arg

* store path in var
  • Loading branch information
erwinmombay authored and Enriqe committed Nov 28, 2018
1 parent 303927a commit 6d32015
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/service/extension-location.js
Expand Up @@ -45,6 +45,15 @@ function calculateScriptBaseUrl(location, opt_isLocalDev) {
return urls.cdn;
}

/**
* Calculates if we need a single pass folder or not.
*
* @return {string}
*/
function getSinglePassExperimentPath() {
return getMode().singlePassType ? `${getMode().singlePassType}/` : '';
}

/**
* Calculate script url for an extension.
* @param {!Location} location The window's location
Expand All @@ -63,7 +72,8 @@ export function calculateExtensionScriptUrl(location, extensionId,
const extensionVersion = opt_extensionVersion
? '-' + opt_extensionVersion
: '';
return `${base}/rtv/${rtv}/v0/${extensionId}${extensionVersion}.js`;
const spPath = getSinglePassExperimentPath();
return `${base}/rtv/${rtv}/${spPath}v0/${extensionId}${extensionVersion}.js`;
}

/**
Expand All @@ -79,7 +89,8 @@ export function calculateEntryPointScriptUrl(
location, entryPoint, isLocalDev, opt_rtv) {
const base = calculateScriptBaseUrl(location, isLocalDev);
if (opt_rtv) {
return `${base}/rtv/${getMode().rtvVersion}/${entryPoint}.js`;
const spPath = getSinglePassExperimentPath();
return `${base}/rtv/${getMode().rtvVersion}/${spPath}${entryPoint}.js`;
}
return `${base}/${entryPoint}.js`;
}
Expand Down
22 changes: 22 additions & 0 deletions test/functional/test-extension-location.js
Expand Up @@ -67,6 +67,17 @@ describes.sandboxed('Extension Location', {}, () => {
expect(script).to.equal(
'http://localhost:8000/dist/rtv/123/v0/no-version.js');
});

it('should handles single pass experiment', () => {
window.AMP_MODE = {rtvVersion: '123', singlePassType: 'sp'};
const script = calculateExtensionScriptUrl({
pathname: 'examples/ads.amp.html',
host: 'localhost:8000',
protocol: 'http:',
}, 'no-version', /* version is empty but defined */ '', true);
expect(script).to.equal(
'http://localhost:8000/dist/rtv/123/sp/v0/no-version.js');
});
});

describe('get correct entry point source', () => {
Expand Down Expand Up @@ -109,6 +120,17 @@ describes.sandboxed('Extension Location', {}, () => {
expect(script).to.equal(
'https://cdn.ampproject.org/rtv/123/ww.js');
});

it('should handle single pass experiment', () => {
window.AMP_MODE = {rtvVersion: '123', singlePassType: 'sp'};
const script = calculateEntryPointScriptUrl({
pathname: 'examples/ads.amp.html',
host: 'localhost:8000',
protocol: 'http:',
}, 'ww', /* isLocalDev */ false, /* opt_rtv */ true);
expect(script).to.equal(
'https://cdn.ampproject.org/rtv/123/sp/ww.js');
});
});

describe('get correct URL parts', () => {
Expand Down

0 comments on commit 6d32015

Please sign in to comment.