Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I disable cache in puppeteer?

I want to disable cache in puppeteer, can anyone please tell me how I can do so? I found this page.setCacheEnabled(enabled) but I couldn't understand how to use the same.

I am aware that the browser is launched without cache or cookies but in my case the browser is always running in the background thus need a different solution.

like image 862
Nagarjun Prasad Avatar asked Feb 13 '18 07:02

Nagarjun Prasad


People also ask

Does puppeteer use cache?

In the latest version of Puppeteer, the request-interception function inconveniently disables the native cache and significantly slows down the actor. Therefore, it's not recommended to follow the examples shown in this article. Puppeteer now uses a native cache that should work well enough for most use cases.

How do I disable frontend local cache?

Here's how... When you're in Google Chrome, click on View, then select Developer, then Developer Tools. Alternatively, you can right click on a page in Chrome, then click Inspect. Click on the Network tab, then check the box to Disable cache.

How do I clear cookies from my puppeteer?

While brand new browser sessions with both Playwright and Puppeteer will not contain any cookies by default, there might be points when it is necessary to clear them. In case you need to clear cookies, you can use page. deleteCookie(...

Can puppeteer run in browser?

Puppeteer is a Node library which provides a high-level API to control Chrome or Chromium over the DevTools Protocol. Puppeteer runs headless by default, but can be configured to run full (non-headless) Chrome or Chromium.


2 Answers

According to the puppeteer docs you can use await page.setCacheEnabled(enabled)

This was added back in December. See Git Hub issue #1609

If you look at the commit changes there is a test e.g.

await page.goto(SOMEURL);

await page.reload({waitUntil: 'networkidle2'});
expect(responses.get('one-style.css').fromCache()).toBe(true);

await page.setCacheEnabled(false);
await page.reload({waitUntil: 'networkidle2'});
expect(responses.get('one-style.css').fromCache()).toBe(false);
like image 123
Rippo Avatar answered Sep 18 '22 18:09

Rippo


If you want session isolation, there is also: const context = await browser.createIncognitoBrowserContext(); const page = await context.newPage(); which will give you a fresh start on each page.

like image 32
reflog Avatar answered Sep 20 '22 18:09

reflog