-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new_audit: add no-unload-listeners audit (#11085)
- Loading branch information
1 parent
439596e
commit ca5b892
Showing
12 changed files
with
416 additions
and
5 deletions.
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 { | ||
/** | ||
* @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; | ||
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.