What is the issue or error you’re encountering
I need a method of reusing a browser rendering session an arbitrary number of times simultaneously
What steps have you taken to resolve the issue?
This is not an error but I can only use the format it gives me.
I am approximately using this example: Deploy a Browser Rendering Worker with Durable Objects | Browser Rendering docs
My goal is to allow any number of workers, which may be more than the browser API session limit of 2, to concurrently access a single browser rendering session. I attempted to create pages in the durable object and pass these back to my worker, but this fails due to the page not being serializable.
Is there any way I can have multiple workers connecting to different pages within a single browser rendering session?
export class Browser extends DurableObject {
constructor(state, env) {
super(state, env);
this.state = state;
this.env = env;
this.keptAliveInSeconds = 0;
this.storage = this.state.storage;
}
async request(){
console.log('createPage');
//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.MYBROWSER);
} catch (e) {
console.log(
`Browser DO: Could not start browser instance. Error: ${e}`,
);
}
}
// Reset keptAlive after each call to the DO
this.keptAliveInSeconds = 0;
console.log('launched');
const page = await this.browser.newPage();
// Reset keptAlive after performing tasks to the DO.
this.keptAliveInSeconds = 0;
// set the first alarm to keep DO alive
let currentAlarm = await this.storage.getAlarm();
if (currentAlarm == null) {
console.log(`Browser DO: setting alarm`);
const TEN_SECONDS = 10 * 1000;
await this.storage.setAlarm(Date.now() + TEN_SECONDS);
}
return page; // this can't be serialized and fails
}