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: reacts urlForAssets loading non js,css files #1554

Merged
merged 9 commits into from
Aug 18, 2017
16 changes: 12 additions & 4 deletions app/react/src/server/iframe.html.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import url from 'url';

const getExtensionForFilename = filename => /.+\.(\w+)$/.exec(filename)[1];

// assets.preview will be:
// - undefined
// - string e.g. 'static/preview.9adbb5ef965106be1cc3.bundle.js'
Expand All @@ -21,7 +23,6 @@ export const urlsFromAssets = assets => {
css: [],
};

const re = /.+\.(\w+)$/;
Object.keys(assets)
// Don't load the manager script in the iframe
.filter(key => key !== 'manager')
Expand All @@ -30,9 +31,16 @@ export const urlsFromAssets = assets => {
if (!Array.isArray(assetList)) {
assetList = [assetList];
}
assetList.filter(assetUrl => re.exec(assetUrl)[1] !== 'map').forEach(assetUrl => {
urls[re.exec(assetUrl)[1]].push(assetUrl);
});
assetList
.filter(assetUrl => {
const extension = getExtensionForFilename(assetUrl);
const isMap = extension === 'map';
const isSupportedExtension = Boolean(urls[extension]);
return isSupportedExtension && !isMap;
})
.forEach(assetUrl => {
urls[getExtensionForFilename(assetUrl)].push(assetUrl);
});
});

return urls;
Expand Down
10 changes: 10 additions & 0 deletions app/react/src/server/iframe.html.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,14 @@ describe('server.urlsFromAssets', () => {
css: ['static/preview.y.css'],
});
});

it('should not return non-js or non-css assets', () => {
const fixture = {
'some-thing.svg': 'some-thing.svg',
};
expect(urlsFromAssets(fixture)).toEqual({
js: [],
css: [],
});
});
});
22 changes: 17 additions & 5 deletions app/vue/src/server/iframe.html.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import url from 'url';

const getExtensionForFilename = filename => /.+\.(\w+)$/.exec(filename)[1];

// assets.preview will be:
// - undefined
// - string e.g. 'static/preview.9adbb5ef965106be1cc3.bundle.js'
Expand All @@ -21,17 +23,27 @@ const urlsFromAssets = assets => {
css: [],
};

const re = /.+\.(\w+)$/;
Object.keys(assets)
// Don't load the manager script in the iframe
.filter(key => key !== 'manager')
.forEach(key => {
const asset = assets[key];
let asset = assets[key];
if (typeof asset === 'string') {
urls[re.exec(asset)[1]].push(asset);
urls[getExtensionForFilename(asset)].push(asset);
} else {
const assetUrl = asset.find(u => re.exec(u)[1] !== 'map');
urls[re.exec(assetUrl)[1]].push(assetUrl);
if (!Array.isArray(asset)) {
asset = [asset];
}
asset
.filter(assetUrl => {
const extension = getExtensionForFilename(assetUrl);
const isMap = extension === 'map';
const isSupportedExtension = Boolean(urls[extension]);
return isSupportedExtension && !isMap;
})
.forEach(assetUrl => {
urls[getExtensionForFilename(assetUrl)].push(assetUrl);
});
}
});

Expand Down