I'm running playwright in a react app. My goal is to return results right away for cached results.
The following is able to set and get cache. But the next time the same URL is requested, it acts like it's not saved in cache. What am I missing?
const { chromium } = require('playwright');
const LRU = require('lru-cache');
const cache = new LRU({
max: 500, // Max number of entries in the cache
ttl: 86400 // Time to live for an entry in the cache, in milliseconds
});
async function fetchHTML(url) {
const browser = await chromium.launch();
const context = await browser.newContext();
const page = await context.newPage();
page.setDefaultTimeout(10000);
// Check if the URL is in the cache
let html = cache.get(url);
if (html) {
// This is NEVER called. 😞
console.log('Retrieved HTML from cache');
} else {
await page.goto(url);
// html = await page.content();
html = await page.evaluate(() => document.body.innerHTML);
console.log('Setting cache for ' + url);
cache.set(url, html);
let cachedHtml = cache.get(url);
// 😄 This works though. 🤷
console.log('Cached HTML length: ' + cachedHtml.length); // Returns: Cached HTML length: 1234
}
await browser.close();
return html;
}
let url = 'https://example.com';
let html = fetchHTML(url); // successful fetch!
console.log(html.length); // Returns: 1234
// but is it cached?
let cachedHtml = fetchHTML(url); // this also fetches, but not from cache. Why?
console.log(cachedHtml.length);