Skip to content

Commit

Permalink
feat(requestinterception): remove cacheSafe flag (#7217)
Browse files Browse the repository at this point in the history
  • Loading branch information
Androbin committed May 20, 2021
1 parent 97c9fe2 commit d01aa6c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 29 deletions.
5 changes: 2 additions & 3 deletions docs/api.md
Expand Up @@ -174,7 +174,7 @@
* [page.setGeolocation(options)](#pagesetgeolocationoptions)
* [page.setJavaScriptEnabled(enabled)](#pagesetjavascriptenabledenabled)
* [page.setOfflineMode(enabled)](#pagesetofflinemodeenabled)
* [page.setRequestInterception(value[, cacheSafe])](#pagesetrequestinterceptionvalue-cachesafe)
* [page.setRequestInterception(value)](#pagesetrequestinterceptionvalue)
* [page.setUserAgent(userAgent)](#pagesetuseragentuseragent)
* [page.setViewport(viewport)](#pagesetviewportviewport)
* [page.tap(selector)](#pagetapselector)
Expand Down Expand Up @@ -2218,10 +2218,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[, cacheSafe])
#### page.setRequestInterception(value)

- `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
13 changes: 2 additions & 11 deletions src/common/NetworkManager.ts
Expand Up @@ -114,7 +114,6 @@ export class NetworkManager extends EventEmitter {
_credentials?: Credentials = null;
_attemptedAuthentications = new Set<string>();
_userRequestInterceptionEnabled = false;
_userRequestInterceptionCacheSafe = false;
_protocolRequestInterceptionEnabled = false;
_userCacheDisabled = false;
_emulatedNetworkConditions: InternalNetworkConditions = {
Expand Down Expand Up @@ -228,12 +227,8 @@ export class NetworkManager extends EventEmitter {
await this._updateProtocolCacheDisabled();
}

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

Expand All @@ -258,11 +253,7 @@ export class NetworkManager extends EventEmitter {
}

_cacheDisabled(): boolean {
return (
this._userCacheDisabled ||
(this._userRequestInterceptionEnabled &&
!this._userRequestInterceptionCacheSafe)
);
return this._userCacheDisabled;
}

async _updateProtocolCacheDisabled(): Promise<void> {
Expand Down
11 changes: 2 additions & 9 deletions src/common/Page.ts
Expand Up @@ -766,8 +766,6 @@ 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},
Expand Down Expand Up @@ -797,13 +795,8 @@ export class Page extends EventEmitter {
* })();
* ```
*/
async setRequestInterception(
value: boolean,
cacheSafe = false
): Promise<void> {
return this._frameManager
.networkManager()
.setRequestInterception(value, cacheSafe);
async setRequestInterception(value: boolean): Promise<void> {
return this._frameManager.networkManager().setRequestInterception(value);
}

/**
Expand Down
15 changes: 9 additions & 6 deletions test/requestinterception.spec.ts
Expand Up @@ -495,13 +495,14 @@ describe('request interception', function () {
expect(urls.has('one-style.html')).toBe(true);
expect(urls.has('one-style.css')).toBe(true);
});
it('should not cache if not cache-safe', async () => {
it('should not cache if cache disabled', async () => {
const { page, server } = getTestState();

// Load and re-load to make sure it's cached.
await page.goto(server.PREFIX + '/cached/one-style.html');

await page.setRequestInterception(true, false);
await page.setRequestInterception(true);
await page.setCacheEnabled(false);
page.on('request', (request) => request.continue());

const cached = [];
Expand All @@ -510,13 +511,14 @@ describe('request interception', function () {
await page.reload();
expect(cached.length).toBe(0);
});
it('should cache if cache-safe', async () => {
it('should cache if cache enabled', async () => {
const { page, server } = getTestState();

// Load and re-load to make sure it's cached.
await page.goto(server.PREFIX + '/cached/one-style.html');

await page.setRequestInterception(true, true);
await page.setRequestInterception(true);
await page.setCacheEnabled(true);
page.on('request', (request) => request.continue());

const cached = [];
Expand All @@ -525,10 +527,11 @@ describe('request interception', function () {
await page.reload();
expect(cached.length).toBe(1);
});
it('should load fonts if cache-safe', async () => {
it('should load fonts if cache enabled', async () => {
const { page, server } = getTestState();

await page.setRequestInterception(true, true);
await page.setRequestInterception(true);
await page.setCacheEnabled(true);
page.on('request', (request) => request.continue());

await page.goto(server.PREFIX + '/cached/one-style-font.html');
Expand Down

0 comments on commit d01aa6c

Please sign in to comment.