Skip to content

Instantly share code, notes, and snippets.

@niieani
Last active October 12, 2021 07:49
Show Gist options
  • Save niieani/8bad67851bc53194d450355229a4c58f to your computer and use it in GitHub Desktop.
Save niieani/8bad67851bc53194d450355229a4c58f to your computer and use it in GitHub Desktop.
Add networkIdleIgnoredUrlPatterns to Lighthouse
diff --git a/lighthouse-core/config/constants.js b/lighthouse-core/config/constants.js
index 264402bb026484a293a9efac60405c52eb8f525b..4aa5098162b70d83ed4ba6c630894ebaf27908a9 100644
--- a/lighthouse-core/config/constants.js
+++ b/lighthouse-core/config/constants.js
@@ -114,6 +114,7 @@ const defaultSettings = {
budgets: null,
locale: 'en-US', // actual default determined by Config using lib/i18n
blockedUrlPatterns: null,
+ networkIdleIgnoredUrlPatterns: null,
additionalTraceCategories: null,
extraHeaders: null,
precomputedLanternData: null,
@@ -133,6 +134,7 @@ const defaultPassConfig = {
networkQuietThresholdMs: 0,
cpuQuietThresholdMs: 0,
blockedUrlPatterns: [],
+ networkIdleIgnoredUrlPatterns: [],
blankPage: 'about:blank',
gatherers: [],
};
@@ -148,6 +150,7 @@ const defaultNavigationConfig = {
networkQuietThresholdMs: 0,
cpuQuietThresholdMs: 0,
blockedUrlPatterns: [],
+ networkIdleIgnoredUrlPatterns: [],
blankPage: 'about:blank',
artifacts: [],
};
diff --git a/lighthouse-core/gather/driver/navigation.js b/lighthouse-core/gather/driver/navigation.js
index 26e5ac22de0f20f6b584be437d417b9d3303aa3f..ad06a3a38e354fca021ba78d33c5b66fae5f17c8 100644
--- a/lighthouse-core/gather/driver/navigation.js
+++ b/lighthouse-core/gather/driver/navigation.js
@@ -86,7 +86,7 @@ async function gotoURL(driver, url, options) {
const networkMonitor = new NetworkMonitor(driver.defaultSession);
// Enable the events and network monitor needed to track navigation progress.
- await networkMonitor.enable();
+ await networkMonitor.enable(options);
await session.sendCommand('Page.enable');
await session.sendCommand('Page.setLifecycleEventsEnabled', {enabled: true});
diff --git a/lighthouse-core/gather/driver/network-monitor.js b/lighthouse-core/gather/driver/network-monitor.js
index 778f8a17930097b0937e8163d87048bc3af4d024..f8a3c38478fcdcb122f54c64e71212a35d671d5d 100644
--- a/lighthouse-core/gather/driver/network-monitor.js
+++ b/lighthouse-core/gather/driver/network-monitor.js
@@ -31,6 +31,7 @@ class NetworkMonitor {
_targetManager = undefined;
/** @type {Array<LH.Crdp.Page.Frame>} */
_frameNavigations = [];
+ _networkIdleIgnoredUrlPatterns = [];
/** @param {LH.Gatherer.FRProtocolSession} session */
constructor(session) {
@@ -84,9 +85,10 @@ class NetworkMonitor {
/**
* @return {Promise<void>}
*/
- async enable() {
+ async enable(options = {}) {
if (this._targetManager) return;
+ this._networkIdleIgnoredUrlPatterns = options.networkIdleIgnoredUrlPatterns || [];
this._frameNavigations = [];
this._sessions = new Map();
this._networkRecorder = new NetworkRecorder();
@@ -131,6 +133,8 @@ class NetworkMonitor {
this._networkRecorder = undefined;
this._targetManager = undefined;
this._sessions = new Map();
+
+ this._networkIdleIgnoredUrlPatterns = [];
}
/** @return {Promise<string | undefined>} */
@@ -152,7 +156,7 @@ class NetworkMonitor {
*/
getInflightRequests() {
if (!this._networkRecorder) return [];
- return this._networkRecorder.getRawRecords().filter(request => !request.finished);
+ return this._networkRecorder.getRawRecords().filter(request => !request.finished && !this.isNetworkIdleIgnored(request));
}
/**
@@ -186,7 +190,19 @@ class NetworkMonitor {
* Returns whether the network is semi-idle (i.e. there are 2 or fewer inflight network requests).
*/
is2Idle() {
- return this._isActiveIdlePeriod(2);
+ return this._isActiveIdlePeriod(
+ 2,
+ request => request.endTime === -1
+ );
+ }
+
+ /**
+ * Returns whether the request should be ignored.
+ * @param {NetworkRequest} request
+ * @return {boolean}
+ */
+ isNetworkIdleIgnored(request) {
+ return this._networkIdleIgnoredUrlPatterns.some(ignoredPattern => request.url.includes(ignoredPattern))
}
/**
@@ -203,6 +219,7 @@ class NetworkMonitor {
for (let i = 0; i < requests.length; i++) {
const request = requests[i];
+ if (this.isNetworkIdleIgnored(request)) continue;
if (request.finished) continue;
if (requestFilter && !requestFilter(request)) continue;
if (NetworkRequest.isNonNetworkRequest(request)) continue;
diff --git a/types/config.d.ts b/types/config.d.ts
index a532b6b03d67c0485923519723fab74c05b3c250..f112f96a08e0c1fbff7bde4d19d61c8bbd901d2c 100644
--- a/types/config.d.ts
+++ b/types/config.d.ts
@@ -85,6 +85,8 @@ declare module Config {
cpuQuietThresholdMs?: number;
/** Substring patterns of network resources to block during this navigation, '*' wildcards supported though unnecessary as prefix or suffix (due to substring matching). */
blockedUrlPatterns?: string[];
+ /** Substring patterns of network resources that shouldn't be considered when computing idleness during this navigation, '*' wildcards supported though unnecessary as prefix or suffix (due to substring matching). */
+ networkIdleIgnoredUrlPatterns?: string[];
/** The URL to use for the "blank" neutral page in between navigations. Defaults to `about:blank`. */
blankPage?: string;
}
diff --git a/types/lhr/settings.d.ts b/types/lhr/settings.d.ts
index 858ac6a62df17a36666b5f8e8d5997000b6525ed..ae20d3632c144db36106ff2dbce30947d46bbc5b 100644
--- a/types/lhr/settings.d.ts
+++ b/types/lhr/settings.d.ts
@@ -61,6 +61,8 @@ export type ScreenEmulationSettings = {
maxWaitForLoad?: number;
/** List of URL patterns to block. */
blockedUrlPatterns?: string[] | null;
+ /** List of URL patterns that shouldn't be considered when computing network idleness. */
+ networkIdleIgnoredUrlPatterns?: string[] | null;
/** Comma-delimited list of trace categories to include. */
additionalTraceCategories?: string | null;
/** Flag indicating the run should only audit. */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment