Reputation: 329
I'm trying to run headless chromium with local cache so web pages that I visited before load faster.
I'm using userDataDir: "C:\Users\user\AppData\Local\Chromium\User Data" for the local cache and checking if its loaded with cache with console.log(response.fromCache());.
const puppeteer = require('puppeteer');
async function test() {
const browser = await puppeteer.launch({
headless: true,
args: ['--no-sandbox'],
userDataDir: "C:\\Users\\user\\AppData\\Local\\Chromium\\User Data"
});
const page = await browser.newPage();
const response = await page.goto('https://example.com');
console.log(response.fromCache());
await browser.close();
}
test();
First time running: console.log, with cache empty, says false.
Second time running: console.log says true.
Until here all is as expected, but then I changed the url to ("https://google.com").
First time running: console.log, with cache empty, says false.
Second time running: console.log says false.
I tried this with multiple urls and until now only the ("https://example.com") has been working with cached browser.
I also tried using headless: false and the console.log continues to say false at second try but I noticed that the second try it actually is using the cache because it loads pretty instantly.
Any ideas why? Or am I doing something wrong. Thanks.
Upvotes: 2
Views: 5845
Reputation: 2933
Puppeteer is likely just following the HTTP caching rules here.
https://example.com, as of this July 23, 2020, returns the following cache headers:
Cache-Control
max-age=604800
Etag
"3147526947+gzip"
Expires
Thu, 30 Jul 2020 13:47:37 GMT
Last-Modified
Thu, 17 Oct 2019 07:18:26 GMT
Which says that the web page can be cached for some time.
Meanwhile, https://google.com returns:
cache-control
private, max-age=0
expires
-1
Which says that the web page should not be cached.
It is fairly common for websites to leave their top-level HTML page uncached, and only use caching on static assets like images, CSS, and JS bundles. This is likely the case with Google's site, which is why the 2nd load is still fast.
Upvotes: 1