diff --git a/lighthouse-core/gather/driver/network-monitor.js b/lighthouse-core/gather/driver/network-monitor.js index bbc6c86405fe..495daae6c57b 100644 --- a/lighthouse-core/gather/driver/network-monitor.js +++ b/lighthouse-core/gather/driver/network-monitor.js @@ -106,10 +106,17 @@ class NetworkMonitor { this._networkRecorder.on('requestloaded', reEmit('requestloaded')); this._session.on('Page.frameNavigated', this._onFrameNavigated); - this._targetManager.addTargetAttachedListener(this._onTargetAttached); - await this._session.sendCommand('Page.enable'); - await this._targetManager.enable(); + + // Legacy driver does its own target management. + // @ts-expect-error + const isLegacyRunner = Boolean(this._session._domainEnabledCounts); + if (isLegacyRunner) { + this._session.addProtocolMessageListener(this._onProtocolMessage); + } else { + this._targetManager.addTargetAttachedListener(this._onTargetAttached); + await this._targetManager.enable(); + } } /** @@ -119,13 +126,21 @@ class NetworkMonitor { if (!this._targetManager) return; this._session.off('Page.frameNavigated', this._onFrameNavigated); - this._targetManager.removeTargetAttachedListener(this._onTargetAttached); - for (const session of this._sessions.values()) { - session.removeProtocolMessageListener(this._onProtocolMessage); - } + // Legacy driver does its own target management. + // @ts-expect-error + const isLegacyRunner = Boolean(this._session._domainEnabledCounts); + if (isLegacyRunner) { + this._session.removeProtocolMessageListener(this._onProtocolMessage); + } else { + this._targetManager.removeTargetAttachedListener(this._onTargetAttached); - await this._targetManager.disable(); + for (const session of this._sessions.values()) { + session.removeProtocolMessageListener(this._onProtocolMessage); + } + + await this._targetManager.disable(); + } this._frameNavigations = []; this._networkRecorder = undefined;