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.
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.
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.
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(...
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.
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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With