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

fix: when using CDN assets, skip local asset URLs #1576

Merged
merged 2 commits into from
Mar 28, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion packages/subapp-server/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ function getDefaultRouteOptions() {
prodBundleBase: "/js",
devBundleBase: "/js",
cspNonceValue: undefined,
templateFile: Path.join(__dirname, "..", "resources", "index-page")
templateFile: Path.join(__dirname, "..", "resources", "index-page"),
cdn: {}
};
}

Expand Down
46 changes: 27 additions & 19 deletions packages/subapp-web/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,49 +121,57 @@ const utils = {
}
*/

mapCdnAssets(bundlesById, basePath = "", assetsFile = "config/assets.json") {
if (!CDN_ASSETS) {
const assetsFp = Path.resolve(assetsFile);
try {
CDN_ASSETS = require(assetsFp);
} catch (err) {
CDN_ASSETS = {};
}
}

mapCdnAssets(bundlesById, basePath = "", cdnAssets) {
const cdnBundles = {};

for (const id in bundlesById) {
const bundles = bundlesById[id];
[].concat(bundles).forEach(bundleFile => {
if (!bundleFile) return;

for (const mapName in CDN_ASSETS) {
if (mapName.endsWith(bundleFile)) {
cdnBundles[id] = CDN_ASSETS[mapName];
break;
let assetUrl;

if (cdnAssets) {
// lookup asset URL from CDN mapping
for (const mapName in cdnAssets) {
if (mapName.endsWith(bundleFile)) {
assetUrl = cdnAssets[mapName];
break;
}
}
} else {
// make asset URL by joining bundleFile with basePath
assetUrl = Path.posix.join(basePath, bundleFile);
}

const baseFilePath = Path.posix.join(basePath, bundleFile);
if (!cdnBundles[id]) {
// set a simple string
cdnBundles[id] = baseFilePath;
cdnBundles[id] = assetUrl;
} else {
// multiple assets for bundle, set and concat as an array
cdnBundles[id] = [].concat(cdnBundles[id], baseFilePath);
cdnBundles[id] = [].concat(cdnBundles[id], assetUrl);
}
});
}

return cdnBundles;
},

getCdnJsBundles(assets, routeOptions, cdnAssetsFile) {
getCdnJsBundles(assets, routeOptions, cdnAssetsFile = "config/assets.json") {
if (CDN_JS_BUNDLES) {
return CDN_JS_BUNDLES;
}

if (routeOptions.cdn.enable !== false && CDN_ASSETS === undefined) {
try {
const assetsFp = Path.resolve(cdnAssetsFile);
CDN_ASSETS = require(assetsFp);
} catch (err) {
console.error("Error: Loading CDN assets map failed", err); // eslint-disable-line
CDN_ASSETS = false;
}
}

//
// pack up entrypoints data from stats
//
Expand All @@ -174,7 +182,7 @@ const utils = {

const bundleBase = utils.getBundleBase(routeOptions);
const allChunks = _.mergeWith({}, js, css, (a, b) => (a && b ? [].concat(a, b) : a || b));
return (CDN_JS_BUNDLES = utils.mapCdnAssets(allChunks, bundleBase, cdnAssetsFile));
return (CDN_JS_BUNDLES = utils.mapCdnAssets(allChunks, bundleBase, CDN_ASSETS));
},

getChunksById(stats) {
Expand Down
1 change: 1 addition & 0 deletions packages/subapp-web/test/spec/init.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe("init", function() {
const initToken = init({
routeOptions: {
__internals: {},
cdn: {},
stats: Path.join(__dirname, "../data/prod-stats.json")
}
});
Expand Down
10 changes: 6 additions & 4 deletions packages/subapp-web/test/spec/util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ describe("loadAssetsFromStats", () => {
});

it("should throw if stats.json does not exist", () => {
expect(
() => loadAssetsFromStats("some path")
).to.throw();
expect(() => loadAssetsFromStats("some path")).to.throw();
});
});

Expand Down Expand Up @@ -91,7 +89,11 @@ describe("getCdnJsBundles", function() {
it("should generate mapping of chunk ID to CDN URLs", () => {
resetCdn();
const { assets } = loadAssetsFromStats(Path.join(__dirname, "../data/prod-stats.json"));
const cdnJsBundles = getCdnJsBundles(assets, {}, "test/data/cdn-assets.json");
const cdnJsBundles = getCdnJsBundles(
assets,
{ cdn: { enable: true } },
"test/data/cdn-assets.json"
);
expect(cdnJsBundles[7]).contains("http://cdnasset.com/hash-123.js");
});
});
Expand Down