Skip to content

Commit

Permalink
elide inline content for js minified audit, add inline attribute to s…
Browse files Browse the repository at this point in the history
…cripts artifact
  • Loading branch information
connorjclark committed Feb 27, 2019
1 parent 9fc4e6d commit cc8e401
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
11 changes: 7 additions & 4 deletions lighthouse-core/audits/byte-efficiency/unminified-javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ class UnminifiedJavaScript extends ByteEfficiencyAudit {

/**
* @param {string} scriptContent
* @param {boolean} inline
* @param {LH.Artifacts.NetworkRequest|undefined} networkRecord
* @return {{url: string, totalBytes: number, wastedBytes: number, wastedPercent: number}}
*/
static computeWaste(scriptContent, networkRecord) {
static computeWaste(scriptContent, inline, networkRecord) {
const contentLength = scriptContent.length;
const totalTokenLength = computeTokenLength(scriptContent);

Expand All @@ -61,7 +62,9 @@ class UnminifiedJavaScript extends ByteEfficiencyAudit {
const wastedBytes = Math.round(totalBytes * wastedRatio);

return {
url: networkRecord ? networkRecord.url : '?',
url: networkRecord && !inline ?
networkRecord.url :
`inline: ${scriptContent.substr(0, 40)}...`,
totalBytes,
wastedBytes,
wastedPercent: 100 * wastedRatio,
Expand All @@ -77,12 +80,12 @@ class UnminifiedJavaScript extends ByteEfficiencyAudit {
/** @type {Array<LH.Audit.ByteEfficiencyItem>} */
const items = [];
const warnings = [];
for (const {requestId, content} of artifacts.Scripts) {
for (const {requestId, inline, content} of artifacts.Scripts) {
if (!content) continue;
const networkRecord = networkRecords.find(record => record.requestId === requestId);

try {
const result = UnminifiedJavaScript.computeWaste(content, networkRecord);
const result = UnminifiedJavaScript.computeWaste(content, inline, networkRecord);
// If the ratio is minimal, the file is likely already minified, so ignore it.
// If the total number of bytes to be saved is quite small, it's also safe to ignore.
if (result.wastedPercent < IGNORE_THRESHOLD_IN_PERCENT ||
Expand Down
2 changes: 2 additions & 0 deletions lighthouse-core/gather/gatherers/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class Scripts extends Gatherer {
...inlineScripts.map(content => {
return {
content,
inline: true,
requestId: mainResource ? mainResource.requestId : undefined,
};
})
Expand All @@ -61,6 +62,7 @@ class Scripts extends Gatherer {
if (content) {
scripts.push({
content,
inline: false,
requestId: record.requestId,
});
}
Expand Down
2 changes: 1 addition & 1 deletion types/artifacts.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ declare global {
/** Set of exceptions thrown during page load. */
RuntimeExceptions: Crdp.Runtime.ExceptionThrownEvent[];
/** The content of all scripts loaded by the page, and the networkRecord requestId that loaded them. Note, HTML documents will have one entry per script tag, all with the same requestId. */
Scripts: Array<{content: string, requestId?: string}>;
Scripts: Array<{content: string, requestId?: string, inline: boolean}>;
/** Version information for all ServiceWorkers active after the first page load. */
ServiceWorker: {versions: Crdp.ServiceWorker.ServiceWorkerVersion[], registrations: Crdp.ServiceWorker.ServiceWorkerRegistration[]};
/** The status of an offline fetch of the page's start_url. -1 and a explanation if missing or there was an error. */
Expand Down

0 comments on commit cc8e401

Please sign in to comment.