How can I disable cache in puppeteer?

元气小坏坏 提交于 2020-05-11 06:33:25

问题


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.


回答1:


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);



回答2:


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.




回答3:


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);



回答4:


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.



来源:https://stackoverflow.com/questions/48761951/how-can-i-disable-cache-in-puppeteer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!