Reputation: 914
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.
Upvotes: 16
Views: 50217
Reputation: 28999
You can use cdpSession.send()
to disable cache:
const client = await page.target().createCDPSession();
await client.send('Network.setCacheDisabled', {
cacheDisabled: true,
});
Alternatively, you can use the more readable page.setCacheEnabled()
:
await page.setCacheEnabled(false);
Upvotes: 7
Reputation: 7645
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.
Upvotes: 22
Reputation: 22424
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);
Upvotes: 31
Reputation: 815
Every browser launch starts with clean HTTP cache and without any cookies.
let browser = await puppeteer.launch(); // no cache, no cookies!
You may try this. For my cases without cache , i am using this.
Upvotes: 2