-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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 no-unload-listeners audit #11085
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c29c7d3
new_audit: add no-unload-listeners audit
brendankenny 73738de
strings and smoke test
brendankenny 784c6bd
switch to source-location details
brendankenny 1fcaf1d
GlobalListeners
brendankenny ff2efc3
fixes
brendankenny 4e71cb3
add no-unload unit tests
brendankenny e5685e1
feedback
brendankenny File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/** | ||
* @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'; | ||
|
||
const Audit = require('./audit.js'); | ||
const i18n = require('./../lib/i18n/i18n.js'); | ||
|
||
const UIStrings = { | ||
/** Descriptive title of a Lighthouse audit that checks if a web page has 'unload' event listeners and finds none. */ | ||
title: 'Avoids `unload` event listeners', | ||
/** Descriptive title of a Lighthouse audit that checks if a web page has 'unload' event listeners and finds that it is using them. */ | ||
failureTitle: 'Registers an `unload` listener', | ||
/** Description of a Lighthouse audit that tells the user why pages should not use the 'unload' event. This is displayed after a user expands the section to see more. 'Learn More' becomes link text to additional documentation. */ | ||
description: 'The `unload` event does not fire reliably and listening for it can prevent browser optimizations like the Back-Forward Cache. Consider using the `pagehide` or `visibilitychange` events instead. [Learn More](https://developers.google.com/web/updates/2018/07/page-lifecycle-api#the-unload-event)', | ||
}; | ||
|
||
const str_ = i18n.createMessageInstanceIdFn(__filename, UIStrings); | ||
|
||
class NoUnloadListeners extends Audit { | ||
/** | ||
* @return {LH.Audit.Meta} | ||
*/ | ||
static get meta() { | ||
return { | ||
id: 'no-unload-listeners', | ||
title: str_(UIStrings.title), | ||
failureTitle: str_(UIStrings.failureTitle), | ||
description: str_(UIStrings.description), | ||
requiredArtifacts: ['GlobalListeners', 'JsUsage'], | ||
}; | ||
} | ||
|
||
/** | ||
* @param {LH.Artifacts} artifacts | ||
* @return {LH.Audit.Product} | ||
*/ | ||
static audit(artifacts) { | ||
const unloadListeners = artifacts.GlobalListeners.filter(l => l.type === 'unload'); | ||
if (!unloadListeners.length) { | ||
return { | ||
score: 1, | ||
}; | ||
} | ||
|
||
/** @type {LH.Audit.Details.Table['headings']} */ | ||
const headings = [ | ||
{key: 'source', itemType: 'source-location', text: str_(i18n.UIStrings.columnURL)}, | ||
]; | ||
|
||
// Look up scriptId to script URL via the JsUsage artifact. | ||
/** @type {Array<[string, string]>} */ | ||
const scriptIdToUrlEntries = Object.values(artifacts.JsUsage) | ||
.reduce((acc, usage) => acc.concat(usage), []) // single-level arr.flat(). | ||
.map(usage => [usage.scriptId, usage.url]); | ||
const scriptIdToUrl = new Map(scriptIdToUrlEntries); | ||
|
||
/** @type {Array<{source: LH.Audit.Details.ItemValue}>} */ | ||
const tableItems = unloadListeners.map(listener => { | ||
const url = scriptIdToUrl.get(listener.scriptId); | ||
|
||
// If we can't find a url, still show something so the user can manually | ||
// look for where an `unload` handler is being created. | ||
if (!url) { | ||
return { | ||
source: { | ||
type: 'url', | ||
value: '(unknown)', | ||
}, | ||
}; | ||
} | ||
|
||
return { | ||
source: { | ||
type: 'source-location', | ||
url, | ||
urlProvider: 'network', | ||
line: listener.lineNumber, | ||
column: listener.columnNumber, | ||
}, | ||
}; | ||
}); | ||
|
||
return { | ||
score: 0, | ||
details: Audit.makeTableDetails(headings, tableItems), | ||
}; | ||
} | ||
} | ||
|
||
module.exports = NoUnloadListeners; | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* @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 | ||
* A gatherer to collect information about the event listeners registered on the | ||
* global object. For now, the scope is narrowed to events that occur on and | ||
* around page unload, but this can be expanded in the future. | ||
*/ | ||
|
||
const Gatherer = require('./gatherer.js'); | ||
|
||
class GlobalListeners extends Gatherer { | ||
brendankenny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** | ||
* @param {LH.Crdp.DOMDebugger.EventListener} listener | ||
* @return {listener is {type: 'pagehide'|'unload'|'visibilitychange'} & LH.Crdp.DOMDebugger.EventListener} | ||
*/ | ||
static _filterForAllowlistedTypes(listener) { | ||
return listener.type === 'pagehide' || | ||
listener.type === 'unload' || | ||
listener.type === 'visibilitychange'; | ||
} | ||
|
||
/** | ||
* @param {LH.Gatherer.PassContext} passContext | ||
* @return {Promise<LH.Artifacts['GlobalListeners']>} | ||
*/ | ||
async afterPass(passContext) { | ||
const driver = passContext.driver; | ||
|
||
// Get a RemoteObject handle to `window`. | ||
const {result: {objectId}} = await driver.sendCommand('Runtime.evaluate', { | ||
expression: 'window', | ||
returnByValue: false, | ||
}); | ||
if (!objectId) { | ||
throw new Error('Error fetching information about the global object'); | ||
} | ||
|
||
// And get all its listeners of interest. | ||
const {listeners} = await driver.sendCommand('DOMDebugger.getEventListeners', {objectId}); | ||
return listeners | ||
.filter(GlobalListeners._filterForAllowlistedTypes) | ||
.map(listener => { | ||
const {type, scriptId, lineNumber, columnNumber} = listener; | ||
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'm just noticing this is already back in node-land so serialization isn't the reason for reexposing, just trying to limit what we return? 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 |
||
return { | ||
type, | ||
scriptId, | ||
lineNumber, | ||
columnNumber, | ||
}; | ||
}); | ||
} | ||
} | ||
|
||
module.exports = GlobalListeners; |
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,77 @@ | ||
/** | ||
* @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'; | ||
|
||
const NoUnloadListeners = require('../../audits/no-unload-listeners.js'); | ||
|
||
/* eslint-env jest */ | ||
|
||
const testJsUsage = { | ||
'https://example.com/1.js': [ | ||
{scriptId: '12', url: 'https://example.com/1.js', functions: []}, | ||
{scriptId: '13', url: 'https://example.com/1.js', functions: []}, | ||
{scriptId: '16', url: 'https://example.com/1.js', functions: []}, | ||
{scriptId: '17', url: 'https://example.com/1.js', functions: []}, | ||
], | ||
'https://example.com/2.js': [ | ||
{scriptId: '22', url: 'https://example.com/2.js', functions: []}, | ||
{scriptId: '23', url: 'https://example.com/2.js', functions: []}, | ||
{scriptId: '26', url: 'https://example.com/2.js', functions: []}, | ||
{scriptId: '27', url: 'https://example.com/2.js', functions: []}, | ||
], | ||
}; | ||
|
||
describe('No Unload Listeners', () => { | ||
it('passes when there were no listeners', () => { | ||
const artifacts = {JsUsage: testJsUsage, GlobalListeners: []}; | ||
const result = NoUnloadListeners.audit(artifacts); | ||
expect(result).toEqual({score: 1}); | ||
}); | ||
|
||
it('passes when there were no `unload` listeners', () => { | ||
const GlobalListeners = [{ | ||
type: 'DOMContentLoaded', scriptId: '12', lineNumber: 5, columnNumber: 0, | ||
}]; | ||
const artifacts = {JsUsage: testJsUsage, GlobalListeners}; | ||
const result = NoUnloadListeners.audit(artifacts); | ||
expect(result).toEqual({score: 1}); | ||
}); | ||
|
||
it('fails when there are unload listeners and matches them to script locations', () => { | ||
const GlobalListeners = [ | ||
{type: 'unload', scriptId: '16', lineNumber: 10, columnNumber: 30}, | ||
{type: 'unload', scriptId: '23', lineNumber: 0, columnNumber: 0}, | ||
]; | ||
const artifacts = {JsUsage: testJsUsage, GlobalListeners}; | ||
const result = NoUnloadListeners.audit(artifacts); | ||
expect(result.score).toEqual(0); | ||
expect(result.details.items).toMatchObject([ | ||
{ | ||
source: {type: 'source-location', url: 'https://example.com/1.js', urlProvider: 'network', line: 10, column: 30}, | ||
}, { | ||
source: {type: 'source-location', url: 'https://example.com/2.js', urlProvider: 'network', line: 0, column: 0}, | ||
}, | ||
]); | ||
}); | ||
|
||
it('fails when there are unload listeners and has a fallback if script URL is not found', () => { | ||
const GlobalListeners = [ | ||
{type: 'DOMContentLoaded', scriptId: '12', lineNumber: 5, columnNumber: 0}, | ||
{type: 'unload', scriptId: 'notascriptid', lineNumber: 10, columnNumber: 30}, | ||
{type: 'unload', scriptId: '22', lineNumber: 1, columnNumber: 100}, | ||
]; | ||
const artifacts = {JsUsage: testJsUsage, GlobalListeners}; | ||
const result = NoUnloadListeners.audit(artifacts); | ||
expect(result.score).toEqual(0); | ||
expect(result.details.items).toMatchObject([ | ||
{ | ||
source: {type: 'url', value: '(unknown)'}, | ||
}, { | ||
source: {type: 'source-location', url: 'https://example.com/2.js', urlProvider: 'network', line: 1, column: 100}, | ||
}, | ||
]); | ||
}); | ||
}); |
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.
feels like it could be a diagnostic too, any particular decision insights behind best practices we can leave for future git blame archeologists? :)
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 guess it kind of falls in with
uses-long-cache-ttl
as something diagnosing performance, just not the performance of any of the metrics just measured (since a Lighthouse run isn't (usually) with a warm cache or on a hit of the back button). I guess I could see the argument that it's still worthwhile to have there, but I chose this as the uncontroversial spot to put it. I don't feel strongly :)