After following a discussion at the GatsbyJS discord space about running puppeter using a cloud function. I decided to give it a try and in this post I will share my experience.
Puppeteer is a Node library which provides a high-level API to control Chrome or Chromium over the DevTools Protocol. Puppeteer runs headless by default.
This library comes with everything you need to:
For te purpose of tihs example we are going to take advantage of the screenshot generation.
For the purpose of this example. I will be using a Cloud Function from Google Firebase service
To use Puppeteer in your project using npm
execute on your functions directory:
The following example creates a new http
function to handle the request using the <https://path-to-your-project.web.app>/screeshot
url. This returns a screenshot of the provided url
query parameter.
Note the
runWith
method is used in order to set timeout and memory allocation used on the cloud function execution.
url
and using the default width
and height
values /screenshot?url=https://jmolivas.com/
url
and width
and height
values /screenshot?url=https://jmolivas.com/&width=1200&height=1600
/screenshot?url=https://jmolivas.com/&full=true