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
Merged

Request interception and caching compatibility #6996

merged 8 commits into from Mar 17, 2021

Conversation

Androbin
Copy link
Contributor

@Androbin Androbin commented Mar 16, 2021

See #2905

This change is fully backwards compatible as it defaults to the current behavior.

@google-cla google-cla bot added the cla: yes label Mar 16, 2021
@Androbin
Copy link
Contributor Author

Androbin commented Mar 16, 2021

This is not a perfect solution, as outlined in #2905 (comment). It should cover most people's use cases though, while we are waiting for Chromium changes to land.

@OrKoN OrKoN requested a review from jschfflr March 17, 2021 06:57
@OrKoN
Copy link
Collaborator

OrKoN commented Mar 17, 2021

Jan, could you PTAL since you looked into the request interception recently?

Copy link
Contributor

@jschfflr jschfflr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@jschfflr
Copy link
Contributor

One thing though: Could you add a unit test to cover this use case?

Copy link
Contributor

@jschfflr jschfflr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah damn, I'm confused by this UI.
Could you add a test that covers this new behaviour?

@Androbin
Copy link
Contributor Author

When we call Network.setCacheDisabled({cacheDisabled: false}), certain events are not fired, including Page.Events.Response. To get around this, the test is listening to the newly added Page.Events.RequestServedFromCache instead.

@jschfflr jschfflr merged commit 8695759 into puppeteer:main Mar 17, 2021
@Androbin Androbin deleted the request-interception-cache-safe branch March 17, 2021 15:04
@jribbens
Copy link

Can I suggest that the documentation update should give the merest clue as to why you might or might not want caching to be disabled? If I'm understanding it correctly, the only problem is that if caching is not disabled, then requests served from the cache will not be intercepted - which may well not be a problem at all in many applications.

@starrify
Copy link
Contributor

I'm under the impression that caching is purposely disabled when configuring request interception because otherwise unexpected issues would happen. This is also stated by the original author @aslushnikov of the relevant code in #1154 and #4260.

It is relatively easy to just configure request interception and having caching enabled at the same time. I have tried that some time ago and have got the similar results (rendering some pages never finish due to lack of the load event in page) as described in this comment: #5176 (comment) . (sorry I'm unable to share further details on my previous tests)

Unfortunately, this PR does not seem to resolve the earlier issue as it merely provides the means for users to keep caching enabled while doing request interception.

I have performed further tests, and it seems that Puppeteer at 8695759 (revision that has this PR merged) would fail to render many webpages, one example of which is https://github.com/, while having page.setRequestInterception(true, true);. I have created issue #7038 to share further details.

I'm afraid that this PR may need to be excluded from the next Puppeteer release unless properly fixed, as the current version merely reintroduced the issue that was previously got rid of in #1154 and #4260.

@Androbin
Copy link
Contributor Author

Androbin commented Mar 31, 2021

@starrify I am fully aware that page.setRequestInterception(true, true); might misbehave for some use cases.
However there are people with use cases where this doesn't break and they really need this flag.
It's opt-in, so there is no harm in keeping the flag. We can however document it as experimental.

@starrify
Copy link
Contributor

Right, I agree that people with specific use cases may be interested in this, though I much doubt the coverage as there are many real-world web pages that would break on this, according to my previous tests and what's mentioned here: #5176 (comment)

@Androbin: Could you help quickly verify whether it is true that Puppeteer at revision 8695759 (with this PR merged) would fail to render https://github.com/ ? (click me for a sample test script)
(async () => {
  const puppeteer = require('puppeteer');
  const browser = await puppeteer.launch({
    // Needed only when inside a Docker container
    args: ['--no-sandbox'],
  });
  const page = await browser.newPage();
  const enableCaching = process.env.TEST_ENABLE_CACHING == 'true';
  await page.setRequestInterception(true, enableCaching);
  page.on('request', (request) => {
    request.continue();
  });
  const url = process.env.TEST_URL || 'https://github.com/';
  try {
    await page.goto(url);
  } catch (err) {
    console.error(err);
  }
  await browser.close();
})();

Sorry but I'm against the very idea of have it documented as experimental, as I would like to see from the documents clear warnings that this feature is known to break the rendering of many real world web pages (if my other issue report doesn't get proved wrong).

@Androbin
Copy link
Contributor Author

Androbin commented Apr 1, 2021

const { page, server } = getTestState();
await page.setRequestInterception(true, false);
page.on('request', (request) => request.continue());
await page.goto('https://github.com/', { waitUntil: 'load' });

PASSED

const { page, server } = getTestState();
await page.setRequestInterception(true, true);
page.on('request', (request) => request.continue());
await page.goto('https://github.com/', { waitUntil: 'domcontentloaded' });

PASSED

const { page, server } = getTestState();
await page.setRequestInterception(true, true);
page.on('request', (request) => request.continue());
await page.goto('https://github.com/', { waitUntil: 'load' });

FAILED

const { page, server } = getTestState();
await page.setRequestInterception(true, true);
page.on('request', (request) => {
  // alternative: request.resourceType() == "font"
  if (request.url().endsWith(".woff")) {
    request.abort();
  } else {
    request.continue();
  }
});
await page.goto('https://github.com/', { waitUntil: 'load' });

PASSED

@Androbin
Copy link
Contributor Author

Androbin commented Apr 1, 2021

I would like to note that blocking requests for fonts and images is a common use case of the people who requested this flag.

@Androbin
Copy link
Contributor Author

Androbin commented Apr 1, 2021

@starrify If you provide me with a list of sites that have this issue, I could compile a list of failing resources, we could identify what's common between them, and fix the issue.

@Androbin
Copy link
Contributor Author

Androbin commented Apr 6, 2021

I have done some debugging and it looks like the events generated by Chromium are in a different order for fonts than for other resources like stylesheets.

Stylesheet request:

  • onRequestWillBeSent
  • onRequestPaused (calls request.continue())
  • onRequest
  • onResponseReceived
  • onLoadingFinished

Font request (cache disabled):

  • onRequestPaused
  • onRequestWillBeSent (calls request.continue())
  • onRequest
  • onResponseReceived
  • onLoadingFinished

Font request (cache enabled):

  • onRequestPaused
  • onRequestWillBeSent (calls request.continue())
  • onRequest
  • onRequestPaused (does not call request.continue())
  • request pending indefinitely

Font request (cache enabled + fix):

  • onRequestPaused
  • onRequestWillBeSent (calls request.continue())
  • onRequest
  • onRequestPaused (calls request.continue())
  • onRequest
  • onResponseReceived
  • onLoadingFinished

@jschfflr
Copy link
Contributor

Could you also check a scenario with redirects and how they change the order of events?

OrKoN pushed a commit that referenced this pull request May 6, 2021
gcf-merge-on-green bot pushed a commit to googleapis/gapic-generator-typescript that referenced this pull request Jun 4, 2021
[![WhiteSource Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [puppeteer](https://togithub.com/puppeteer/puppeteer) | [`^9.1.1` -> `^10.0.0`](https://renovatebot.com/diffs/npm/puppeteer/9.1.1/10.0.0) | [![age](https://badges.renovateapi.com/packages/npm/puppeteer/10.0.0/age-slim)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://badges.renovateapi.com/packages/npm/puppeteer/10.0.0/adoption-slim)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://badges.renovateapi.com/packages/npm/puppeteer/10.0.0/compatibility-slim/9.1.1)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://badges.renovateapi.com/packages/npm/puppeteer/10.0.0/confidence-slim/9.1.1)](https://docs.renovatebot.com/merge-confidence/) |

---

### Release Notes

<details>
<summary>puppeteer/puppeteer</summary>

### [`v10.0.0`](https://togithub.com/puppeteer/puppeteer/blob/master/CHANGELOG.md#&#8203;1000-httpsgithubcompuppeteerpuppeteercomparev911v1000-2021-05-31)

[Compare Source](https://togithub.com/puppeteer/puppeteer/compare/v9.1.1...v10.0.0)

##### ⚠ BREAKING CHANGES

-   Node.js 10 is no longer supported.

##### Features

-   **chromium:** roll to Chromium 92.0.4512.0 (r884014) ([#&#8203;7288](https://togithub.com/puppeteer/puppeteer/issues/7288)) ([f863f4b](https://togithub.com/puppeteer/puppeteer/commit/f863f4bfe015e57ea1f9fbb322f1cedee468b857))
-   **requestinterception:** remove cacheSafe flag ([#&#8203;7217](https://togithub.com/puppeteer/puppeteer/issues/7217)) ([d01aa6c](https://togithub.com/puppeteer/puppeteer/commit/d01aa6c84a1e41f15ffed3a8d36ad26a404a7187))
-   expose other sessions from connection ([#&#8203;6863](https://togithub.com/puppeteer/puppeteer/issues/6863)) ([cb285a2](https://togithub.com/puppeteer/puppeteer/commit/cb285a237921259eac99ade1d8b5550e068a55eb))
-   **launcher:** add new launcher option `waitForInitialPage` ([#&#8203;7105](https://togithub.com/puppeteer/puppeteer/issues/7105)) ([2605309](https://togithub.com/puppeteer/puppeteer/commit/2605309f74b43da160cda4d214016e4422bf7676)), closes [#&#8203;3630](https://togithub.com/puppeteer/puppeteer/issues/3630)

##### Bug Fixes

-   added comments for browsercontext, startCSSCoverage, and startJSCoverage. ([#&#8203;7264](https://togithub.com/puppeteer/puppeteer/issues/7264)) ([b750397](https://togithub.com/puppeteer/puppeteer/commit/b75039746ac6bddf1411538242b5e70b0f2e6e8a))

-   modified comment for method product, platform and newPage ([#&#8203;7262](https://togithub.com/puppeteer/puppeteer/issues/7262)) ([159d283](https://togithub.com/puppeteer/puppeteer/commit/159d2835450697dabea6f9adf6e67d158b5b8ae3))

-   **requestinterception:** fix font loading issue ([#&#8203;7060](https://togithub.com/puppeteer/puppeteer/issues/7060)) ([c9978d2](https://togithub.com/puppeteer/puppeteer/commit/c9978d20d5584c9fd2dc902e4b4ac86ed8ea5d6e)), closes [/github.com/puppeteer/puppeteer/pull/6996#issuecomment-811546501](https://togithub.com/puppeteer//github.com/puppeteer/puppeteer/pull/6996/issues/issuecomment-811546501) [/github.com/puppeteer/puppeteer/pull/6996#issuecomment-813797393](https://togithub.com/puppeteer//github.com/puppeteer/puppeteer/pull/6996/issues/issuecomment-813797393) [#&#8203;7038](https://togithub.com/puppeteer/puppeteer/issues/7038)

-   drop support for Node.js 10 ([#&#8203;7200](https://togithub.com/puppeteer/puppeteer/issues/7200)) ([97c9fe2](https://togithub.com/puppeteer/puppeteer/commit/97c9fe2520723d45a5a86da06b888ae888d400be)), closes [#&#8203;6753](https://togithub.com/puppeteer/puppeteer/issues/6753)

##### [9.1.1](https://togithub.com/puppeteer/puppeteer/compare/v9.1.0...v9.1.1) (2021-05-05)

##### Bug Fixes

-   make targetFilter synchronous ([#&#8203;7203](https://togithub.com/puppeteer/puppeteer/issues/7203)) ([bcc85a0](https://togithub.com/puppeteer/puppeteer/commit/bcc85a0969077d122e5d8d2fb5c1061999a8ae48))

</details>

---

### Configuration

📅 **Schedule**: At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Never, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about these updates again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box.

---

This PR has been generated by [WhiteSource Renovate](https://renovate.whitesourcesoftware.com). View repository job log [here](https://app.renovatebot.com/dashboard#github/googleapis/gapic-generator-typescript).
This was referenced Jun 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants