-
Notifications
You must be signed in to change notification settings - Fork 9.5k
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
core(render-blocking): handle amp-style stylesheets #4555
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2d08e3f
core(render-blocking): handle amp-style stylesheets
patrickhulce 3df2c9b
lint
patrickhulce 672ee72
add comment to dbw tester while loop
patrickhulce 32cd97f
feedback
patrickhulce 6b3789a
feedback 2
patrickhulce b5092b6
better smoketests
patrickhulce File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,23 +5,39 @@ | |
*/ | ||
|
||
/** | ||
* @fileoverview | ||
* Identifies stylesheets, HTML Imports, and scripts that potentially block | ||
* the first paint of the page by running several scripts in the page context. | ||
* Candidate blocking tags are collected by querying for all script tags in | ||
* the head of the page and all link tags that are either matching media | ||
* stylesheets or non-async HTML imports. These are then compared to the | ||
* network requests to ensure they were initiated by the parser and not | ||
* injected with script. To avoid false positives from strategies like | ||
* (http://filamentgroup.github.io/loadCSS/test/preload.html), a separate | ||
* script is run to flag all links that at one point were rel=preload. | ||
*/ | ||
* @fileoverview | ||
* Identifies stylesheets, HTML Imports, and scripts that potentially block | ||
* the first paint of the page by running several scripts in the page context. | ||
* Candidate blocking tags are collected by querying for all script tags in | ||
* the head of the page and all link tags that are either matching media | ||
* stylesheets or non-async HTML imports. These are then compared to the | ||
* network requests to ensure they were initiated by the parser and not | ||
* injected with script. To avoid false positives from strategies like | ||
* (http://filamentgroup.github.io/loadCSS/test/preload.html), a separate | ||
* script is run to flag all links that at one point were rel=preload. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const Gatherer = require('../gatherer'); | ||
|
||
/* global document,window */ | ||
/* global document,window,HTMLLinkElement */ | ||
|
||
function installMediaListener() { | ||
window.___linkMediaChanges = []; | ||
Object.defineProperty(HTMLLinkElement.prototype, 'media', { | ||
set: function(val) { | ||
window.___linkMediaChanges.push({ | ||
url: this.href, | ||
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. naming bikesheds? url => href |
||
value: val, | ||
time: Date.now() - window.performance.timing.responseEnd, | ||
matches: window.matchMedia(val).matches, | ||
}); | ||
|
||
return this.setAttribute('media', val); | ||
}, | ||
}); | ||
} | ||
|
||
/* istanbul ignore next */ | ||
function collectTagsThatBlockFirstPaint() { | ||
|
@@ -30,17 +46,19 @@ function collectTagsThatBlockFirstPaint() { | |
const tagList = [...document.querySelectorAll('link, head script[src]')] | ||
.filter(tag => { | ||
if (tag.tagName === 'SCRIPT') { | ||
return !tag.hasAttribute('async') && | ||
!tag.hasAttribute('defer') && | ||
!/^data:/.test(tag.src) && | ||
tag.getAttribute('type') !== 'module'; | ||
return ( | ||
!tag.hasAttribute('async') && | ||
!tag.hasAttribute('defer') && | ||
!/^data:/.test(tag.src) && | ||
tag.getAttribute('type') !== 'module' | ||
); | ||
} | ||
|
||
// Filter stylesheet/HTML imports that block rendering. | ||
// https://www.igvita.com/2012/06/14/debunking-responsive-css-performance-myths/ | ||
// https://www.w3.org/TR/html-imports/#dfn-import-async-attribute | ||
const blockingStylesheet = (tag.rel === 'stylesheet' && | ||
window.matchMedia(tag.media).matches && !tag.disabled); | ||
const blockingStylesheet = | ||
tag.rel === 'stylesheet' && window.matchMedia(tag.media).matches && !tag.disabled; | ||
const blockingImport = tag.rel === 'import' && !tag.hasAttribute('async'); | ||
return blockingStylesheet || blockingImport; | ||
}) | ||
|
@@ -53,6 +71,7 @@ function collectTagsThatBlockFirstPaint() { | |
rel: tag.rel, | ||
media: tag.media, | ||
disabled: tag.disabled, | ||
mediaChanges: window.___linkMediaChanges.filter(item => item.url === tag.href), | ||
}; | ||
}); | ||
resolve(tagList); | ||
|
@@ -99,17 +118,30 @@ class TagsBlockingFirstPaint extends Gatherer { | |
|
||
static findBlockingTags(driver, networkRecords) { | ||
const scriptSrc = `(${collectTagsThatBlockFirstPaint.toString()}())`; | ||
const firstRequestEndTime = networkRecords.reduce( | ||
(min, record) => Math.min(min, record._endTime), | ||
Infinity | ||
); | ||
return driver.evaluateAsync(scriptSrc).then(tags => { | ||
const requests = filteredAndIndexedByUrl(networkRecords); | ||
|
||
return tags.reduce((prev, tag) => { | ||
const request = requests[tag.url]; | ||
if (request && !request.isLinkPreload) { | ||
// Even if the request was initially blocking or appeared to be blocking once the | ||
// page was loaded, the media attribute could have been changed during load, capping the | ||
// amount of time it was render blocking. See https://github.com/GoogleChrome/lighthouse/issues/2832. | ||
const nonMatchingMediaChangeTimes = (tag.mediaChanges || []) | ||
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.
|
||
.filter(change => !change.matches) | ||
.map(change => change.time); | ||
const earliestNonBlockingTime = Math.min(...nonMatchingMediaChangeTimes); | ||
const lastTimeResourceWasBlocking = firstRequestEndTime + earliestNonBlockingTime / 1000; | ||
|
||
prev.push({ | ||
tag, | ||
transferSize: request.transferSize || 0, | ||
startTime: request.startTime, | ||
endTime: request.endTime, | ||
endTime: Math.min(request.endTime, lastTimeResourceWasBlocking), | ||
}); | ||
|
||
// Prevent duplicates from showing up again | ||
|
@@ -122,12 +154,19 @@ class TagsBlockingFirstPaint extends Gatherer { | |
} | ||
|
||
/** | ||
* @param {!Object} options | ||
* @param {!Object} context | ||
*/ | ||
beforePass(context) { | ||
return context.driver.evaluteScriptOnNewDocument(`(${installMediaListener.toString()})()`); | ||
} | ||
|
||
/** | ||
* @param {!Object} context | ||
* @param {{networkRecords: !Array<!NetworkRecord>}} tracingData | ||
* @return {!Array<{tag: string, transferSize: number, startTime: number, endTime: number}>} | ||
*/ | ||
afterPass(options, tracingData) { | ||
return TagsBlockingFirstPaint.findBlockingTags(options.driver, tracingData.networkRecords); | ||
afterPass(context, tracingData) { | ||
return TagsBlockingFirstPaint.findBlockingTags(context.driver, tracingData.networkRecords); | ||
} | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 use an add'l stylesheet with delay=5000 instead? Seems more natural.
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.
unfortunately, no. since the only way we assert that we didn't flag the amp stylesheet is by asserting the overall render-blocking stylesheets value is <3s. in the future details, but extendedInfo-style world this will be easier :)