Skip to content
AnswerWell geo Documentation

Research · 7 min read · Answerwell

Headless Browser Run: Puppeteer Bindings, Quick Actions, Screenshots, and Local Development

How Cloudflare Browser Run works with Workers bindings, Puppeteer, Playwright, Quick Actions, limits, guardrails, and local development.

This page summarises Cloudflare Browser Run documentation: running managed headless Chromium from Cloudflare Workers for screenshots, PDFs, HTML extraction, crawling, and multi-step automation. Control paths include Quick Actions on the browser binding, Puppeteer or Playwright inside a Worker (@cloudflare/puppeteer, @cloudflare/playwright), CDP from your machine with puppeteer-core / playwright-core, and optional tools such as Stagehand or external Browserbase via WebSocket.

Browser Run binding: what it gives a Worker

Section titled “Browser Run binding: what it gives a Worker”

A browser binding in Wrangler connects your Worker to a headless Chromium instance Cloudflare manages. Typical configuration:

{
"browser": {
"binding": "BROWSER"
}
}

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.

— Cloudflare, Deploy a Browser Run Worker

The same binding name (for example env.MYBROWSER or env.BROWSER) is passed into puppeteer.launch() or Playwright’s launch(). If launch fails with Cannot read properties of undefined (reading ‘fetch’), the binding was not passed into puppeteer.launch — see the Browser Run FAQ.

Browser tool for agents describes Browser Run as a way to run browser automation on Cloudflare, often combined with Queues, KV, R2, or AI bindings in one Worker.

Quick Actions vs Puppeteer and Playwright: when to use which

Section titled “Quick Actions vs Puppeteer and Playwright: when to use which”

Browser Run supports two main styles of use, plus full automation libraries.

Approach Best for Trade-off
quickAction("screenshot" | "content" | …) One-shot screenshot, rendered HTML, simple captures Less customization; Quick Actions need a remote browser binding and are not fully validated with local emulation
@cloudflare/puppeteer / @cloudflare/playwright Navigation, clicks, page.evaluate(), PDFs, tests, crawlers Requires nodejs_compat and a recent compatibility_date for Puppeteer; you manage browser lifecycle
CDP from Node (puppeteer-core, playwright-core) CI, local scripts against Browser Run API API token with Browser Rendering edit permission

You can generate PDFs with Browser Run in two ways: Quick Actions: Use the /pdf endpoint. This is ideal if you do not need to customize rendering behavior. Puppeteer or Playwright: Use browser automation within Workers for additional control and customization.

— Cloudflare, Generate PDFs Using HTML and CSS

A minimal Quick Action Worker:

export default {
async fetch(request, env) {
return await env.BROWSER.quickAction("screenshot", {
url: "https://example.com",
});
},
};

That pattern is from Get started with Browser Run. For rendered HTML (pre-rendering, indexing), quickAction("content", …) returns JSON whose result field holds the HTML — see Pre-render pages for crawlers.

Google’s upstream library is the same conceptual model Workers use:

Puppeteer is a JavaScript library developed by Google that provides a high-level API to control Chrome over the Chrome DevTools Protocol (CDP) or WebDriver BiDi.

— Google, Automation and testing with Chrome

On Workers, Cloudflare’s fork connects to Browser Run instead of a local Chrome binary — Puppeteer on Browser Run.

Puppeteer and Playwright in a Worker: launch pattern and limits

Section titled “Puppeteer and Playwright in a Worker: launch pattern and limits”

Install the Workers-specific package and launch with the binding:

npm i -D @cloudflare/puppeteer

The Workers team forked a version of Puppeteer and patched it to connect to the Workers Browser Run API instead. After connecting, the developers can then use the full Puppeteer API as they would on a standard setup.

— Cloudflare, Puppeteer

Typical flow: puppeteer.launch(env.MYBROWSER) → newPage() → goto() or setContent() → screenshot, page.pdf(), or page.evaluate(). Playwright on Browser Run follows the same binding pattern with @cloudflare/playwright.

Workers-specific constraints:

  • XPath selectors in Puppeteer are not supported in the Workers runtime; use CSS selectors or run XPath inside page.evaluate() — Puppeteer element selection.
  • page.evaluate() should return primitives (strings, numbers, booleans), not live DOM nodes — important for brand kit extraction.
  • Puppeteer needs nodejs_compat and compatibility_date of 2025-09-15 or later (brand-kit tutorial uses 2026-10-29) — Extract a brand kit.

Under load, prefer reusing sessions with puppeteer.sessions() and puppeteer.connect(), calling disconnect() rather than close() — Reuse sessions. Durable Objects can keep a browser warm (~60 seconds after last use) — Browser Run with Durable Objects.

puppeteer.launch() may fail when you exceed browsers per account; the Queues crawler tutorial wraps launch in try/catch and calls batch.retryAll() on failure.

Common tasks: screenshot, PDF, crawl, pre-render, brand data

Section titled “Common tasks: screenshot, PDF, crawl, pre-render, brand data”

The deploy Worker guide shows a ?url= handler that screenshots a URL, stores JPEG in KV with TTL, and serves cache hits without reopening the browser. Capture a screenshot (Gateway middleware) uses quickAction("screenshot", …), saves PNG to R2, and notes that screenshots consume Browser Run usage — scope or sample captures.

Generate PDFs Using HTML and CSS uses page.setContent() with styled HTML, then page.pdf({ printBackground: true }). Avoid blind interpolation of untrusted input into HTML in production.

Custom fonts for PDF or screenshots: inject @font-face via page.addStyleTag() — Custom fonts and the PDF endpoint note. Pre-installed fonts are listed in Supported fonts; missing fonts fall back unless you inject them.

Build a web crawler with Queues and Browser Run combines puppeteer.launch(env.CRAWLER_BROWSER), robots.txt checks via robots-parser, KV de-duplication (e.g. skip re-crawl within an hour), and optional recursive sendBatch (watch Durable Object subrequest depth limits).

Pre-render pages for crawlers validates allowed hostnames, then quickAction("content") with waitUntil: "networkidle2". Fetch and index single web pages pairs Browser Run with AI Search bindings in one Worker.

Extract a brand kit from any website reads computed styles in the browser (text-only models miss real colors/fonts). Use page.setBypassCSP(true) and networkidle2 before page.evaluate(). Generate OG images for Astro sites screenshots a fixed-size Astro route (1200×630).

More patterns: Browser Run examples and how-to tutorials.

Local development: wrangler dev, remote bindings, and security

Section titled “Local development: wrangler dev, remote bindings, and security”

Recent Wrangler releases provision a browser for local dev so the same browser binding often works locally and when deployed — Browser agent local development.

Mode Behavior
Default npx wrangler dev Local browser spin-up; Quick Actions may need --remote
"remote": true on browser binding Real managed headless Chrome during dev — Wrangler reference
npx wrangler dev --remote Worker runs against Cloudflare network; required for some Browser Run tests — brand kit production notes
X_BROWSER_HEADFUL=true Visible Chrome for debugging (experimental) — Browser Run changelog

The .quickAction() method is not yet supported in local development mode. Use wrangler dev –remote when testing Browser Run Quick Actions locally.

— Cloudflare, Pre-render pages for crawlers

Local dev limitations include requests larger than 1 MB not supported — FAQ. Use cdpUrl only when you intentionally connect to another CDP endpoint (tunnel or manual Chrome) — Browser agent.

Pass guardrails at puppeteer.launch() (Playwright accepts the same object):

const browser = await puppeteer.launch(env.MYBROWSER, {
guardrails: {
allowedDomains: ["example.com", "*.example.com"],
allowedDomainSets: ["common-cdns"],
},
});

Blocked navigations return 403 with cf-mitigated: guardrails and cf-brapi-guardrails-reason: not-in-allowlist — Guardrails.

For user-supplied URLs, allow only http: / https:, block private and link-local ranges to reduce SSRF, and restrict hostnames in application code — brand kit production considerations and pre-render tutorial. Respect robots.txt when crawling — Queues crawler.

CDP from outside Workers: create a session via the Browser Run DevTools API and connect with puppeteer-core — prerequisites and install steps are in Using with Puppeteer (CDP) and Using with Playwright (CDP). Connect Puppeteer from your local machine is the in-platform entry point.

  • Calling puppeteer.launch() without the browser binding.
  • Testing Quick Actions only with fully local emulation instead of --remote or remote: true.
  • Treating Miniflare as a security boundary for untrusted Worker code.
  • Open proxy Workers that accept any ?url= without hostname allowlists or guardrails.
  • Relying on text-only extraction for colors, fonts, or layout (use real browser DOM / computed styles).
  1. Add a browser binding in Wrangler and deploy a minimal Worker from Get started or Deploy a Browser Run Worker.
  2. For Quick Actions, run npx wrangler dev --remote and request your local URL; confirm a screenshot or HTML response — Pre-render pages.
  3. For Puppeteer, hit /?url=https://example.com (or your test URL) and verify image or JSON output; repeat to confirm KV cache behavior if you implemented it.
  4. If using guardrails, navigate to a blocked host and confirm 403 and guardrail headers — Guardrails.
  5. You can check automatically in Answerwell (our tool): https://answerwell.org/workspace

If something on this page is wrong or unclear, email yimasynthetic@gmail.com with the page URL and the exact statement you are asking about so it can be corrected.