-
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
new_audit: add preload-fonts audit #11255
Merged
Merged
Changes from all commits
Commits
Show all changes
54 commits
Select commit
Hold shift + click to select a range
6c57f87
barebones boiler for new audit
lemcardenas 2a8533b
added boiler test file
lemcardenas dabd7ba
fleshing out future audit
lemcardenas e240ff3
Merge branch 'master' into jankless-font-audit
lemcardenas 8297def
more small changes
lemcardenas 5d76ce2
added new audit to default for testing
lemcardenas 11b267b
still experimenting with font-display
lemcardenas 174f7e7
tmp env
lemcardenas ab5acac
basic working audit
lemcardenas 5c43a17
changes to attempted URLs
lemcardenas f9da1f0
reverted changes to check for indirect link preloads
lemcardenas e98473d
cleaned up the audit code
lemcardenas 7aa0cdb
renamed audit
lemcardenas 1c0ccf4
Merge branch 'master' into jankless-font-audit
lemcardenas 1d05617
working basic tests
lemcardenas bad337a
more tests
lemcardenas 6e5c3f8
more test changes
lemcardenas 90961d2
more tests
lemcardenas af7cd7a
added title and description
lemcardenas 655e600
reverted changes in uses-rel-preload
lemcardenas a6f650e
removed tmp env
lemcardenas 27fdcb4
changed variable names
lemcardenas d400aa1
Merge branch 'master' into jankless-font-audit
lemcardenas fa4be5d
updated cli index test snap
lemcardenas d3a3a9a
changed the learn more link
lemcardenas b940ff4
updated strings
lemcardenas c189a6a
deleted unfeasible TODO
lemcardenas 52bc2c4
added comment about document.fonts.load
lemcardenas 5016379
added smoke test, refactored audit to use shorthand prop names
lemcardenas 5b2098a
Merge branch 'master' into jankless-font-audit
lemcardenas dd42bdb
fixed nits
lemcardenas 0983f35
Merge branch 'master' into jankless-font-audit
lemcardenas 0e96a83
Merge remote-tracking branch 'origin/master' into jankless-font-audit
connorjclark 651111b
moved audit to best-practices-ux group
lemcardenas afe8fb7
fixed default config for new audit group
lemcardenas d884255
Merge branch 'master' into jankless-font-audit
lemcardenas 8555e05
Merge branch 'jankless-font-audit' of github.com:GoogleChrome/lightho…
lemcardenas 5507406
fixed issue with index-test snap
lemcardenas e3ffe70
fixed perf test to allow best-practices group for jankless-font
lemcardenas 4846651
updated test since best-practices now has an N/A audit
lemcardenas 116d29a
added comment clarifying jankless-font in smoke perf
lemcardenas 58ffe03
moved clarification comment to config
lemcardenas be83a44
changed audit name to preload-fonts
lemcardenas 2b1dd00
Merge branch 'master' into jankless-font-audit
lemcardenas faa65bd
more name changes
lemcardenas 43c5802
changed the link back to preload-optional-fonts
lemcardenas 7e9524d
updated artifacts so that preload-fonts is not N/A
lemcardenas d7e9974
updated audit description
lemcardenas e58a1e9
Merge branch 'master' into jankless-font-audit
lemcardenas 859f6fc
fixed nits & removed start/endTime in tests
lemcardenas 4584664
reverted a test name
lemcardenas dcb8db0
Merge branch 'master' into jankless-font-audit
lemcardenas 788b0d5
removed superfluous comments/code
lemcardenas d773e20
Merge branch 'master' into jankless-font-audit
lemcardenas 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
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
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 |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/** | ||
* @license Copyright 2020 The Lighthouse Authors. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. | ||
*/ | ||
'use strict'; | ||
|
||
/** | ||
* @fileoverview | ||
* Audit that checks whether fonts that use `font-display: optional` were preloaded. | ||
*/ | ||
|
||
const Audit = require('./audit.js'); | ||
const i18n = require('./../lib/i18n/i18n.js'); | ||
const FontDisplay = require('./../audits/font-display.js'); | ||
const PASSING_FONT_DISPLAY_REGEX = /^(optional)$/; | ||
const NetworkRecords = require('../computed/network-records.js'); | ||
|
||
const UIStrings = { | ||
/** Title of a Lighthouse audit that provides detail on whether fonts that used `font-display: optional` were preloaded. This descriptive title is shown to users when all fonts that used `font-display: optional` were preloaded. */ | ||
title: 'Fonts with `font-display: optional` are preloaded', | ||
/** Title of a Lighthouse audit that provides detail on whether fonts that used `font-display: optional` were preloaded. This descriptive title is shown to users when one or more fonts used `font-display: optional` and were not preloaded. */ | ||
failureTitle: 'Fonts with `font-display: optional` are not preloaded', | ||
/** Description of a Lighthouse audit that tells the user why they should preload fonts if they are using `font-display: optional`. This is displayed after a user expands the section to see more. No character length limits. 'Learn More' becomes link text to additional documentation. */ | ||
description: 'Preload `optional` fonts so first-time visitors may use them. [Learn More](https://web.dev/preload-optional-fonts/)', | ||
}; | ||
|
||
const str_ = i18n.createMessageInstanceIdFn(__filename, UIStrings); | ||
|
||
class PreloadFontsAudit extends Audit { | ||
/** | ||
* @return {LH.Audit.Meta} | ||
*/ | ||
static get meta() { | ||
return { | ||
id: 'preload-fonts', | ||
title: str_(UIStrings.title), | ||
failureTitle: str_(UIStrings.failureTitle), | ||
description: str_(UIStrings.description), | ||
requiredArtifacts: ['devtoolsLogs', 'URL', 'CSSUsage'], | ||
}; | ||
} | ||
|
||
/** | ||
* Finds which font URLs were attempted to be preloaded, | ||
* ignoring those that failed to be reused and were requested again. | ||
* Note: document.fonts.load() is a valid way to preload fonts, | ||
* but we are not currently checking for that. | ||
* @param {Array<LH.Artifacts.NetworkRequest>} networkRecords | ||
* @return {Set<string>} | ||
*/ | ||
static getURLsAttemptedToPreload(networkRecords) { | ||
const attemptedURLs = networkRecords | ||
.filter(req => req.resourceType === 'Font') | ||
.filter(req => req.isLinkPreload) | ||
.map(req => req.url); | ||
|
||
return new Set(attemptedURLs); | ||
} | ||
|
||
/** | ||
* @param {LH.Artifacts} artifacts | ||
* @param {LH.Audit.Context} context | ||
* @return {Promise<LH.Audit.Product>} | ||
*/ | ||
static async audit(artifacts, context) { | ||
const devtoolsLog = artifacts.devtoolsLogs[this.DEFAULT_PASS]; | ||
const networkRecords = await NetworkRecords.request(devtoolsLog, context); | ||
|
||
// Gets the URLs of fonts where font-display: optional. | ||
const optionalFontURLs = | ||
FontDisplay.findFontDisplayDeclarations(artifacts, PASSING_FONT_DISPLAY_REGEX).passingURLs; | ||
|
||
// Gets the URLs of fonts attempted to be preloaded. | ||
const preloadedFontURLs = | ||
PreloadFontsAudit.getURLsAttemptedToPreload(networkRecords); | ||
|
||
const results = Array.from(optionalFontURLs) | ||
.filter(url => !preloadedFontURLs.has(url)) | ||
.map(url => { | ||
return {url}; | ||
}); | ||
|
||
/** @type {LH.Audit.Details.Table['headings']} */ | ||
const headings = [ | ||
{key: 'url', itemType: 'url', text: str_(i18n.UIStrings.columnURL)}, | ||
]; | ||
|
||
return { | ||
score: results.length > 0 ? 0 : 1, | ||
details: Audit.makeTableDetails(headings, results), | ||
notApplicable: optionalFontURLs.size === 0, | ||
}; | ||
} | ||
} | ||
|
||
module.exports = PreloadFontsAudit; | ||
module.exports.UIStrings = UIStrings; |
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
Oops, something went wrong.
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.
i had a concern that
findFontDisplayDeclarations
is rather expensive. and was thinking we could skip it if we saw no font network requests.but.. i looked at some timings on LR for
lh:audit:font-display
(which also does that work) and it seems plenty quick90pctl is 12ms! fast.
so yeah ... i'm cool with this cost.
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.
Hooray for preventative perf checks 💯