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

update CSS byte validation to 75000 #31

Merged
merged 3 commits into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 10 additions & 6 deletions lib/media.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,24 @@ function getContentsOfFiles(fileArray, targetDir, filePrefix) {
* @param {array} fileArray An array of files
* @param {string} directory The directory in which `fs` will look for the file
* @param {string} filePrefix The directory path before the filename
* @param {string} uri of the page requested
* @return {string}
*/
function combineFileContents(fileArray, directory, filePrefix) {
function combineFileContents(fileArray, directory, filePrefix, uri) {
if (!fileArray || !fileArray.length) {
return false;
}

// If there are files, retrieve contents
return getContentsOfFiles(fileArray, directory, filePrefix)
.then(function (fileContents) {
const joined = fileContents.join('');
const joined = fileContents.join(''),
byteLength = util.getByteLength(joined);

if (util.getByteLength(joined) > 50000) {
log('warn', 'Combined CSS contents are beyond the 50000 byte limit specified by AMPHTML.');
if (byteLength > 75000) { // see https://github.com/ampproject/amphtml/issues/26466
log('warn', 'Combined CSS contents are beyond the 75000 byte limit specified by AMPHTML.', { uri, byteLength });
} else if (byteLength > 70000) {
log('info', 'Combined CSS contents are above 70000 bytes, near the 75000 byte limit specified by AMPHTML.', { uri, byteLength });
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if you should put a second warning for articles > 70,000. So we can see how many articles are nearing the the limit too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great point, and I should take the opportunity to add a bit more context to the logged object too


return `<style amp-custom>${joined}</style>`;
Expand Down Expand Up @@ -223,7 +227,7 @@ function configure(options, cacheBuster = '') {
* @return {Function}
*/
function injectStyles(state) {
const { locals } = state;
const { locals, _self } = state;

return function (html) {
var mediaMap = {};
Expand All @@ -239,7 +243,7 @@ function injectStyles(state) {
// allow site to change the media map before applying it
if (setup.resolveMedia) mediaMap = setup.resolveMedia(mediaMap, locals) || mediaMap;

mediaMap.styles = combineFileContents(mediaMap.styles, 'public/css', '/css/');
mediaMap.styles = combineFileContents(mediaMap.styles, 'public/css', '/css/', _self);

return bluebird.props(mediaMap)
.then(combinedFiles => {
Expand Down
4 changes: 2 additions & 2 deletions lib/media.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,14 +331,14 @@ describe(_.startCase(filename), () => {
test('logs when the combined file contents are too large', () => {
jest.spyOn(lib, 'getMediaMap').mockReturnValue(_.cloneDeep(cssMediaMap));
jest.spyOn(util, 'readFilePromise').mockReturnValue(Promise.resolve(styleString));
jest.spyOn(util, 'getByteLength').mockReturnValue(50001);
jest.spyOn(util, 'getByteLength').mockReturnValue(75001);

return fn(state)(basicHtml)
.then(() => {
expect(fakeLog.mock.calls).toEqual([
[
'warn',
'Combined CSS contents are beyond the 50000 byte limit specified by AMPHTML.'
'Combined CSS contents are beyond the 75000 byte limit specified by AMPHTML.'
]
]);
});
Expand Down