Setting up browser caching in Puppeteer with Chrome

I’m working on a Puppeteer project where I want to enable browser caching so pages load quicker on repeat visits. I set up a custom user data directory to store the cache and I’m using response.fromCache() to verify if caching is working properly.

const pptr = require('puppeteer');

async function runTest() {
    const browserInstance = await pptr.launch({
        headless: true,
        args: ['--disable-web-security'],
        userDataDir: "D:\\Browser\\ChromeData"
    });
    
    const newPage = await browserInstance.newPage();
    const pageResponse = await newPage.goto('https://example.com');
    
    console.log('Cache status:', pageResponse.fromCache());
    
    await browserInstance.close();
}
runTest();

When I test with example.com, the first run shows false and the second run shows true which makes sense. However, when I switch to google.com, both the first and second runs return false even though the page seems to load faster on the second attempt when I run it in non-headless mode. This happens with most websites except example.com. What could be causing this inconsistent caching behavior?

I’ve hit this same issue with Puppeteer caching. It’s usually about how different sites handle cache headers. Google and big sites set aggressive cache-control headers that block browser caching or use super short expiration times. They’ll also serve different content based on user agents, timestamps, or other dynamic stuff that breaks caching.

What worked for me was adding --aggressive-cache-discard=false to launch args, plus --disable-features=VizDisplayCompositor. Some sites detect automation and deliberately serve non-cacheable responses to headless browsers.

Try intercepting requests with page.setRequestInterception(true) and log the cache-control headers from different sites. You’ll see exactly why responses aren’t getting cached. That faster loading in non-headless mode? Probably connection reuse, not actual cache hits.

cache behavior’s all over the place between sites. google probably sends different etags or timestamps each time, which breaks caching. Check the response headers - you’ll likely see cache-control: no-cache or really short max-age values. Also, userDataDir gets wiped sometimes depending on how chrome shuts down. Try testing with a persistent session where you don’t fully close the browser.

The problem is Puppeteer’s session and cache handling. Chrome doesn’t properly restore cached resources when you launch new browser instances, even with userDataDir set. I hit this same issue during performance testing last year. Keep your browser instance alive between page visits instead of launching fresh every time. Reuse the same page or create new pages within the same browser session. Also drop --disable-web-security - it messes with normal caching. Google uses dynamic query parameters and kills cache frequently, so fromCache() returning false makes sense. That speed boost you saw in non-headless mode? That’s probably DNS caching and connection pooling, not actual resource caching. Test with a simple static site first to make sure your caching setup actually works.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.