Reuse sessions
The best way to improve the performance of your browser rendering worker is to reuse sessions. One way to do that is via Durable Objects, which allows you to keep a long running connection from a worker to a browser. Another way is to keep the browser open after you’ve finished with it, and connect to that session each time you have a new request.
In short, this entails using browser.disconnect()
instead of browser.close()
, and, if there are available sessions, using puppeteer.connect(env.MY_BROWSER, sessionID)
instead of launching a new browser session.
Cloudflare Workers provides a serverless execution environment that allows you to create new applications or augment existing ones without configuring or maintaining infrastructure. Your Worker application is a container to interact with a headless browser to do actions, such as taking screenshots.
Create a new Worker project named browser-worker
by running:
For setup, select the following options:
- For What would you like to start with?, choose
Hello World example
. - For Which template would you like to use?, choose
Hello World Worker
. - For Which language do you want to use?, choose
TypeScript
. - For Do you want to use git for version control?, choose
Yes
. - For Do you want to deploy your application?, choose
No
(we will be making some changes before deploying).
In your browser-worker
directory, install Cloudflare’s fork of Puppeteer:
The script below starts by fetching the current running sessions. If there are any that don’t already have a worker connection, it picks a random session ID and attempts to connect (puppeteer.connect(..)
) to it. If that fails or there were no running sessions to start with, it launches a new browser session (puppeteer.launch(..)
). Then, it goes to the website and fetches the dom. Once that’s done, it disconnects (browser.disconnect()
), making the connection available to other workers.
Take into account that if the browser is idle, i.e. does not get any command, for more than the current limit, it will close automatically, so you must have enough requests per minute to keep it alive.
Besides puppeteer.sessions()
, we’ve added other methods to facilitate session management - check them out here.
Run npx wrangler dev --remote
to test your Worker remotely before deploying to Cloudflare’s global network. Local mode support does not exist for Browser Rendering so --remote
is required.
To test go to the following URL:
<LOCAL_HOST_URL>/?url=https://example.com
Run npx wrangler deploy
to deploy your Worker to the Cloudflare global network and then to go to the following URL:
<YOUR_WORKER>.<YOUR_SUBDOMAIN>.workers.dev/?url=https://example.com