-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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(unused-css-rules): update to support new coverage format #2518
Changes from 2 commits
4608a68
c56b19f
b669d5e
210c514
57de9c9
a0b414f
237a94f
1e35aa9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ node lighthouse-cli/test/fixtures/static-server.js & | |
|
||
sleep 0.5s | ||
|
||
config="lighthouse-core/config/default.js" | ||
config="lighthouse-core/config/full-config.js" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need to update There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
expectations="lighthouse-cli/test/smokehouse/byte-efficiency/expectations.js" | ||
|
||
yarn smokehouse -- --config-path=$config --expectations-path=$expectations | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,25 +39,22 @@ class UnusedCSSRules extends ByteEfficiencyAudit { | |
indexed[record.url] = record; | ||
return indexed; | ||
}, {}); | ||
|
||
return styles.reduce((indexed, stylesheet) => { | ||
indexed[stylesheet.header.styleSheetId] = Object.assign({ | ||
used: [], | ||
unused: [], | ||
usedRules: [], | ||
networkRecord: indexedNetworkRecords[stylesheet.header.sourceURL], | ||
}, stylesheet); | ||
return indexed; | ||
}, {}); | ||
} | ||
|
||
/** | ||
* Counts the number of unused rules and adds count information to sheets. | ||
* Adds used rules to their corresponding stylesheet. | ||
* @param {!Array.<{styleSheetId: string, used: boolean}>} rules The output of the CSSUsage gatherer. | ||
* @param {!Object} indexedStylesheets Stylesheet information indexed by id. | ||
* @return {number} The number of unused rules. | ||
*/ | ||
static countUnusedRules(rules, indexedStylesheets) { | ||
let unused = 0; | ||
|
||
static indexUsedRules(rules, indexedStylesheets) { | ||
rules.forEach(rule => { | ||
const stylesheetInfo = indexedStylesheets[rule.styleSheetId]; | ||
|
||
|
@@ -66,14 +63,44 @@ class UnusedCSSRules extends ByteEfficiencyAudit { | |
} | ||
|
||
if (rule.used) { | ||
stylesheetInfo.used.push(rule); | ||
} else { | ||
unused++; | ||
stylesheetInfo.unused.push(rule); | ||
stylesheetInfo.usedRules.push(rule); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* @param {!Object} stylesheetInfo | ||
* @param {boolean} isInline | ||
* @return {{debugString: string|undefined, wastedBytes: number, totalBytes: number}} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
*/ | ||
static computeUsage(stylesheetInfo, isInline) { | ||
let usedUncompressedBytes = 0; | ||
const totalUncompressedBytes = stylesheetInfo.content.length; | ||
|
||
for (const usedRule of stylesheetInfo.usedRules) { | ||
usedUncompressedBytes += usedRule.endOffset - usedRule.startOffset; | ||
} | ||
|
||
// If we don't know for sure how many bytes this sheet used on the network, | ||
// we can guess it was roughly the size of the content gzipped. | ||
const totalBytes = stylesheetInfo.networkRecord ? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will this be weird for inline stylesheets that came in a page that was gzipped? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will be weird for inline stylesheets that came in a page that was not gzipped since we'll underestimate the savings. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
ah, yeah, that was based on how it was (I believe) filtering out networkRecords for non-stylesheets before |
||
stylesheetInfo.networkRecord.transferSize : | ||
Math.round(stylesheetInfo.content.length / 3); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since this is the full run, not the default run, instead of estimating like this can we just actually run gzip on it like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we don't actually know it was gzipped anyway I'm inclined to not introduce that complexity here too, but I don't feel that strongly. |
||
|
||
return unused; | ||
const percentUnused = (totalUncompressedBytes - usedUncompressedBytes) / totalUncompressedBytes; | ||
const wastedBytes = Math.round(percentUnused * totalBytes); | ||
|
||
let debugString; | ||
if (!isInline && !stylesheetInfo.networkRecord) { | ||
debugString = `Unable to find network record for ${stylesheetInfo.header.sourceURL}`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I get this error on both paulirish.com and theverge.com.. can we avoid that? |
||
} | ||
|
||
return { | ||
debugString, | ||
wastedBytes, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not worth logging There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah didn't seem worth it anymore, the cases where it happens now are wholly expected and there's no sense being noisy |
||
wastedPercent: percentUnused * 100, | ||
totalBytes, | ||
}; | ||
} | ||
|
||
/** | ||
|
@@ -115,40 +142,25 @@ class UnusedCSSRules extends ByteEfficiencyAudit { | |
/** | ||
* @param {!Object} stylesheetInfo The stylesheetInfo object. | ||
* @param {string} pageUrl The URL of the page, used to identify inline styles. | ||
* @return {{url: string, label: string, code: string}} The result for the details object. | ||
* @return {?{url: string, wastedBytes: number, totalBytes: number}} | ||
*/ | ||
static mapSheetToResult(stylesheetInfo, pageUrl) { | ||
const numUsed = stylesheetInfo.used.length; | ||
const numUnused = stylesheetInfo.unused.length; | ||
|
||
if ((numUsed === 0 && numUnused === 0) || stylesheetInfo.isDuplicate) { | ||
if (stylesheetInfo.isDuplicate) { | ||
return null; | ||
} | ||
|
||
let url = stylesheetInfo.header.sourceURL; | ||
let isInline = false; | ||
if (!url || url === pageUrl) { | ||
const contentPreview = UnusedCSSRules.determineContentPreview(stylesheetInfo.content); | ||
url = '*inline*```' + contentPreview + '```'; | ||
url = {type: 'code', text: contentPreview}; | ||
isInline = true; | ||
} else { | ||
url = URL.getURLDisplayName(url); | ||
} | ||
|
||
// If we don't know for sure how many bytes this sheet used on the network, | ||
// we can guess it was roughly the size of the content gzipped. | ||
const totalBytes = stylesheetInfo.networkRecord ? | ||
stylesheetInfo.networkRecord.transferSize : | ||
Math.round(stylesheetInfo.content.length / 3); | ||
|
||
const percentUnused = numUnused / (numUsed + numUnused); | ||
const wastedBytes = Math.round(percentUnused * totalBytes); | ||
|
||
return { | ||
url, | ||
numUnused, | ||
wastedBytes, | ||
wastedPercent: percentUnused * 100, | ||
totalBytes, | ||
}; | ||
const usage = UnusedCSSRules.computeUsage(stylesheetInfo, isInline); | ||
return Object.assign({url}, usage); | ||
} | ||
|
||
/** | ||
|
@@ -163,21 +175,27 @@ class UnusedCSSRules extends ByteEfficiencyAudit { | |
const devtoolsLogs = artifacts.devtoolsLogs[ByteEfficiencyAudit.DEFAULT_PASS]; | ||
return artifacts.requestNetworkRecords(devtoolsLogs).then(networkRecords => { | ||
const indexedSheets = UnusedCSSRules.indexStylesheetsById(styles, networkRecords); | ||
UnusedCSSRules.countUnusedRules(usage, indexedSheets); | ||
const results = Object.keys(indexedSheets).map(sheetId => { | ||
return UnusedCSSRules.mapSheetToResult(indexedSheets[sheetId], pageUrl); | ||
}).filter(sheet => sheet && sheet.wastedBytes > 1024); | ||
UnusedCSSRules.indexUsedRules(usage, indexedSheets); | ||
|
||
const results = Object.keys(indexedSheets) | ||
.map(sheetId => UnusedCSSRules.mapSheetToResult(indexedSheets[sheetId], pageUrl)) | ||
.filter(sheet => sheet && sheet.wastedBytes > 1024); | ||
|
||
const headings = [ | ||
{key: 'url', itemType: 'url', text: 'URL'}, | ||
{key: 'numUnused', itemType: 'url', text: 'Unused Rules'}, | ||
{key: 'totalKb', itemType: 'text', text: 'Original'}, | ||
{key: 'potentialSavings', itemType: 'text', text: 'Potential Savings'}, | ||
]; | ||
|
||
const debugString = results | ||
.map(result => result.debugString) | ||
.filter(Boolean) | ||
.pop(); | ||
|
||
return { | ||
debugString, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add to return type There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
results, | ||
headings | ||
headings, | ||
}; | ||
}); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we make this more precise since we control the test page?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure, the score gets bucketed by the generic byte efficiency audit though so it's not that useful. I added an assertion on the "wastedKb" instead which is a little more explicit and useful