Skip to content

Commit

Permalink
fix: when using CDN assets, skip local asset URLs (#1576)
Browse files Browse the repository at this point in the history
* fix: when using CDN assets, skip local asset URLs

* still fallback to local URL if nothing found from CDN map
  • Loading branch information
jchip authored Mar 28, 2020
1 parent 0d44b3f commit 1bb6b8a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 24 deletions.
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
48 changes: 29 additions & 19 deletions packages/subapp-web/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,49 +121,59 @@ 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;
}
}
}

const baseFilePath = Path.posix.join(basePath, bundleFile);
if (!assetUrl) {
// make asset URL by joining bundleFile with basePath
assetUrl = 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 +184,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

0 comments on commit 1bb6b8a

Please sign in to comment.