sajad torkamani

In a nutshell

Browserless is a browser automation service that lets you run Chrome instances on the cloud. This means that when using a tool like puppeteer, you can tell puppeteer to use a cloud instance of Chrome:

// Before: launch local Chrome instance
const browser = await puppeteer.launch();

// After: connect to browserless to use remote Chrome instance
const browser = await puppeteer.connect({
  browserWSEndpoint: 'wss://chrome.browserless.io/?token=YOUR_API_TOKEN',
});

Browserless can be useful if you don’t want to manage Chrome instances on your own servers.

REST API

See Swagger docs for all endpoints. Noteworthy endpoints are:

Best practices

Always close sessions

const browser = await puppeteer.connect({
  browserWSEndpoint: `wss://chrome.browserless.io?token=YOUR-TOKEN-HERE`,
});

const page = await browser.newPage();

try {
  await page.goto('https://www.browserless.io/');
  await page.screenshot({ path: './browserless.png' });
  browser.close();
} catch (error) {
  console.error({ error }, 'Something happened!');
  browser.close();
}

Other notes

  • Browserless can:
    • Isolate different Chrome sessions from each other so that if one crashes, the rest aren’t affected.
    • Run concurrent requests / multiple Chrome sessions.
    • Restart automatically if anything crashes.
  • If you have any issues with rendering fonts, check out this post.

Sources

Tagged: Misc