Skip to content

Commit cd21fa3

Browse files
authored
core(stylesheets): disable transient stylesheet detection (#16121)
1 parent 83fbc11 commit cd21fa3

File tree

2 files changed

+10
-40
lines changed

2 files changed

+10
-40
lines changed

core/gather/gatherers/stylesheets.js

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -60,63 +60,32 @@ class Stylesheets extends BaseGatherer {
6060

6161
/**
6262
* @param {LH.Gatherer.Context} context
63+
* @return {Promise<LH.Artifacts['Stylesheets']>}
6364
*/
64-
async startInstrumentation(context) {
65+
async getArtifact(context) {
66+
const executionContext = context.driver.executionContext;
6567
const session = context.driver.defaultSession;
6668
this._session = session;
6769

68-
// Calling `CSS.enable` will emit events for stylesheets currently on the page.
69-
// We want to ignore these events in navigation mode because they are not relevant to the
70-
// navigation that is about to happen. Adding the event listener *after* calling `CSS.enable`
71-
// ensures that the events for pre-existing stylesheets are ignored.
72-
const isNavigation = context.gatherMode === 'navigation';
73-
if (!isNavigation) {
74-
session.on('CSS.styleSheetAdded', this._onStylesheetAdded);
75-
}
70+
session.on('CSS.styleSheetAdded', this._onStylesheetAdded);
7671

7772
await session.sendCommand('DOM.enable');
7873
await session.sendCommand('CSS.enable');
7974

80-
if (isNavigation) {
81-
session.on('CSS.styleSheetAdded', this._onStylesheetAdded);
82-
}
83-
}
84-
75+
// Force style to recompute.
76+
// Doesn't appear to be necessary in newer versions of Chrome.
77+
await executionContext.evaluateAsync('getComputedStyle(document.body)');
8578

86-
/**
87-
* @param {LH.Gatherer.Context} context
88-
*/
89-
async stopInstrumentation(context) {
90-
const session = context.driver.defaultSession;
9179
session.off('CSS.styleSheetAdded', this._onStylesheetAdded);
9280

9381
// Ensure we finish fetching all stylesheet contents before disabling the CSS domain
94-
await Promise.all(this._sheetPromises.values());
82+
const sheets = await Promise.all(this._sheetPromises.values());
9583

9684
await session.sendCommand('CSS.disable');
9785
await session.sendCommand('DOM.disable');
98-
}
99-
100-
/**
101-
* @param {LH.Gatherer.Context} context
102-
* @return {Promise<LH.Artifacts['Stylesheets']>}
103-
*/
104-
async getArtifact(context) {
105-
const executionContext = context.driver.executionContext;
106-
107-
if (context.gatherMode === 'snapshot') {
108-
await this.startInstrumentation(context);
109-
110-
// Force style to recompute.
111-
// Doesn't appear to be necessary in newer versions of Chrome.
112-
await executionContext.evaluateAsync('getComputedStyle(document.body)');
113-
114-
await this.stopInstrumentation(context);
115-
}
11686

11787
/** @type {Map<string, LH.Artifacts.CSSStyleSheetInfo>} */
11888
const dedupedStylesheets = new Map();
119-
const sheets = await Promise.all(this._sheetPromises.values());
12089

12190
for (const sheet of sheets) {
12291
// Erroneous sheets will be reported via sentry and the log.

core/test/gather/gatherers/stylesheets-test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ describe('Stylesheets gatherer', () => {
8585
.mockEvent('CSS.styleSheetAdded', {header: {styleSheetId: '2'}});
8686
context.driver.defaultSession.sendCommand
8787
.mockResponse('DOM.enable')
88-
.mockResponse('CSS.enable')
88+
// @ts-expect-error - Force events to emit.
89+
.mockResponse('CSS.enable', flushAllTimersAndMicrotasks)
8990
.mockResponse('CSS.getStyleSheetText', () => {
9091
throw new Error('Sheet not found');
9192
})

0 commit comments

Comments
 (0)