Misunderstood that, so you can keep it alive for months on end and only pay for the durable object?
Misunderstood that, so you can keep it alive for months on end and only pay for the durable object?
setContent and that html runs some javascript that wants to access a large local json file, can puppeteer somehow access the local filestystem of the worker? Or how would this work?<script> const json = JSON.parse(${rawJson}; ...) and then use page.setContents works, but not sure if this is a performant way to do it.

291b861f09df80526646856f248a8776
Unabled to create new browser: const browser = await pup.launch({
headless: `new`,
defaultViewport: { width: 1920, height: 1080 },
})
const getPageSnapshot = (url, fullPage) => new Promise(async (resolve, reject) => { // this returns a promise resolving to a buffer
const page = await browser.newPage().catch(reject)
await page.goto(url).catch(reject)
resolve(await page.screenshot({
fullPage
}).then(async (r) => { await page.close(); return r }).catch(reject))
}) async handle(
request: Request,
env: any,
context: any,
data: Record<string, any>
) : Promise<Response> {
// some url handling
const browser = await puppeteer.launch(env.MYBROWSER);
const page = await browser.newPage();
await page.setViewport({ width: 1920, height: 1080 })
await page.goto(url);
const screenshot = (await page.screenshot({
fullPage: false,
})) as Buffer;
await browser.close();
return new Response(screenshot, {
headers: {
'Content-Type': `image/jpeg`,
},
});
} await page.goto(url, {
waitUntil: `load`
}); {
"message": [
"Browser DO: Could not start browser instance. Error: Error: Unabled to create new browser: code: 429: message: Too Many Requests",
"Error: Unabled to create new browser: code: 429: message: Too Many Requests\n at PuppeteerWorkers.launch (index.js:19201:13)\n at async Browser2.fetch (index.js:43967:24)"
],
"level": "error",
"timestamp": 1702366528655
} async fetch(url: string) {
//if there's a browser session open, re-use it
if (!this.browser || !this.browser.isConnected()) {
console.log(`Browser DO: Starting new instance`);
try {
this.browser = await puppeteer.launch(this.env.CHROMIUM);
} catch (e) {
console.error(
`Browser DO: Could not start browser instance. Error: ${e}`,
e.stack
);
throw e;
}
}
.......
} async alarm() {
this.keptAliveInSeconds += 10;
// Extend browser DO life
if (this.keptAliveInSeconds < KEEP_BROWSER_ALIVE_IN_SECONDS) {
console.log(
`Browser DO: has been kept alive for ${this.keptAliveInSeconds} seconds. Extending lifespan.`
);
await this.storage.setAlarm(Date.now() + 10 * 1000);
await this.browser.version();
} else
console.log(
`Browser DO: exceeded life of ${KEEP_BROWSER_ALIVE_IN_SECONDS}. Browser DO will be shut down in 10 seconds.`
);
}