Skip to content
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

Request interception and caching compatibility #6996

Merged
merged 8 commits into from Mar 17, 2021
5 changes: 3 additions & 2 deletions docs/api.md
Expand Up @@ -166,7 +166,7 @@
* [page.setGeolocation(options)](#pagesetgeolocationoptions)
* [page.setJavaScriptEnabled(enabled)](#pagesetjavascriptenabledenabled)
* [page.setOfflineMode(enabled)](#pagesetofflinemodeenabled)
* [page.setRequestInterception(value)](#pagesetrequestinterceptionvalue)
* [page.setRequestInterception(value[, cacheSafe])](#pagesetrequestinterceptionvalue-cachesafe)
* [page.setUserAgent(userAgent)](#pagesetuseragentuseragent)
* [page.setViewport(viewport)](#pagesetviewportviewport)
* [page.tap(selector)](#pagetapselector)
Expand Down Expand Up @@ -2039,8 +2039,9 @@ await page.setGeolocation({latitude: 59.95, longitude: 30.31667});
- `enabled` <[boolean]> When `true`, enables offline mode for the page.
- returns: <[Promise]>

#### page.setRequestInterception(value)
#### page.setRequestInterception(value[, cacheSafe])
- `value` <[boolean]> Whether to enable request interception.
- `cacheSafe` <[boolean]> Whether to trust browser caching. If set to false, enabling request interception disables page caching. Defaults to false.
- returns: <[Promise]>

Activating request interception enables `request.abort`, `request.continue` and
Expand Down
11 changes: 9 additions & 2 deletions src/common/NetworkManager.ts
Expand Up @@ -75,6 +75,7 @@ export class NetworkManager extends EventEmitter {
_credentials?: Credentials = null;
_attemptedAuthentications = new Set<string>();
_userRequestInterceptionEnabled = false;
_userRequestInterceptionCacheSafe = false;
_protocolRequestInterceptionEnabled = false;
_userCacheDisabled = false;
_requestIdToInterceptionId = new Map<string, string>();
Expand Down Expand Up @@ -189,8 +190,12 @@ export class NetworkManager extends EventEmitter {
await this._updateProtocolCacheDisabled();
}

async setRequestInterception(value: boolean): Promise<void> {
async setRequestInterception(
value: boolean,
cacheSafe = false
): Promise<void> {
this._userRequestInterceptionEnabled = value;
this._userRequestInterceptionCacheSafe = cacheSafe;
await this._updateProtocolRequestInterception();
}

Expand All @@ -217,7 +222,9 @@ export class NetworkManager extends EventEmitter {
async _updateProtocolCacheDisabled(): Promise<void> {
await this._client.send('Network.setCacheDisabled', {
cacheDisabled:
this._userCacheDisabled || this._userRequestInterceptionEnabled,
this._userCacheDisabled ||
(this._userRequestInterceptionEnabled &&
!this._userRequestInterceptionCacheSafe),
});
}

Expand Down
15 changes: 10 additions & 5 deletions src/common/Page.ts
Expand Up @@ -688,16 +688,16 @@ export class Page extends EventEmitter {

/**
* @param value - Whether to enable request interception.
* @param cacheSafe - Whether to trust browser caching. If set to false,
* enabling request interception disables page caching. Defaults to false.
*
* @remarks
* Activating request interception enables {@link HTTPRequest.abort},
* {@link HTTPRequest.continue} and {@link HTTPRequest.respond} methods. This
* provides the capability to modify network requests that are made by a page.
*
* Once request interception is enabled, every request will stall unless it's
* continued, responded or aborted.
*
* **NOTE** Enabling request interception disables page caching.
* continued, responded or aborted; or completed using the browser cache.
*
* @example
* An example of a naïve request interceptor that aborts all image requests:
Expand All @@ -719,8 +719,13 @@ export class Page extends EventEmitter {
* })();
* ```
*/
async setRequestInterception(value: boolean): Promise<void> {
return this._frameManager.networkManager().setRequestInterception(value);
async setRequestInterception(
value: boolean,
cacheSafe = false
): Promise<void> {
return this._frameManager
.networkManager()
.setRequestInterception(value, cacheSafe);
}

/**
Expand Down