Deploy a Browser Rendering Worker
By following this guide, you will create a Worker that uses the Browser Rendering API to take screenshots from web pages. This is a common use case for browser automation.
- Sign up for a Cloudflare account ↗.
- Install
npm
↗. - Install
Node.js
↗.
Node.js version manager
Use a Node version manager like Volta ↗ or nvm ↗ to avoid permission issues and change Node.js versions. Wrangler, discussed later in this guide, requires a Node version of 16.17.0
or later.
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
JavaScript / 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:
Browser Rendering can be used with other developer products. You might need a relational database, an R2 bucket to archive your crawled pages and assets, a Durable Object to keep your browser instance alive and share it with multiple requests, or Queues to handle your jobs asynchronous.
For the purpose of this guide, you are going to use a KV store to cache your screenshots.
Create two namespaces, one for production, and one for development.
Take note of the IDs for the next step.
Configure your browser-worker
project’s wrangler.toml
file by adding a browser binding and a Node.js compatibility flag. Bindings allow your Workers to interact with resources on the Cloudflare developer platform. Your browser binding
name is set by you, this guide uses the name MYBROWSER
. Browser bindings allow for communication between a Worker and a headless browser which allows you to do actions such as taking a screenshot, generating a PDF and more.
Update your wrangler.toml
configuration file with the Browser Rendering API binding and the KV namespaces you created:
Update src/index.js
with your Worker code:
Update src/worker.ts
with your Worker code:
This Worker instantiates a browser using Puppeteer, opens a new page, navigates to what you put in the "url"
parameter, takes a screenshot of the page, stores the screenshot in KV, closes the browser, and responds with the JPEG image of the screenshot.
If your Worker is running in production, it will store the screenshot to the production KV namespace. If you are running wrangler dev
, it will store the screenshot to the dev KV namespace.
If the same "url"
is requested again, it will use the cached version in KV instead, unless it expired.
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 taking your first screenshot, 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.
To take your first screenshot, go to the following URL:
<YOUR_WORKER>.<YOUR_SUBDOMAIN>.workers.dev/?url=https://example.com
- Other Puppeteer examples ↗