You have unlimited access as a PRO member
You are receiving a free preview of 3 lessons
Your free preview as expired - please upgrade to PRO
Contents
Recent Posts
- Object Oriented Programming With TypeScript
- Angular Elements Advanced Techniques
- TypeScript - the Basics
- The Real State of JavaScript 2018
- Cloud Scheduler for Firebase Functions
- Testing Firestore Security Rules With the Emulator
- How to Use Git and Github
- Infinite Virtual Scroll With the Angular CDK
- Build a Group Chat With Firestore
- Async Await Pro Tips
How to Use Puppeteer With Firebase Cloud Functions
Episode 130 written by Jeff DelaneyHealth Check: This lesson was last reviewed on and tested with these packages:
- CloudFunctions v
Find an issue? Let's fix it
Source code for How to Use Puppeteer With Firebase Cloud Functions on Github
Cloud Functions v2.0 provides support for Puppeteer, which means using headless chrome in a serverless environment is now possible. In the following lesson, you will learn how to setup Puppeteer in a Firebase Function and use it to perform serverside rendering of any site that lives on internet.
Potential use cases for headless chrome in Firebase include:
- Serverside Rendering and Prerendering
- Webcrawlers and Scrapers
- End-to-End and Performance Testing
Setting up Puppeteer in Firebase Cloud Functions
Let’s make sure we have the latest version of the Firebase CLI Tools and initialize functions.
npm install firebase-tools@latest -g |
Using Node8 in a Firebase Function
Puppeteer requires Node8 or greater, so we need to setup that runtime in the package.json
in the functions directory.
{ |
Update your TS config for the DOM
Puppeteer is a web browser, hence it uses the DOM. By default, the tsconfig.json
for functions does not include the the DOM lib - let’s change that.
{ |
Allocate More Memory to a Function
Puppeteer requires a good amount of memory. Let’s setup an HTTP function and give it a higher memory allocation by chaining runWith
.
import * as functions from 'firebase-functions'; |
Notice how the render
function below has 1GB of memory on runs on Node8
Basic Web App Rendering Function
The function below visits a webpage, renders it (including its JavaScript), and responds with the content in that page.
This function is essentially a proxy browser that can be used by passing the desired url as a query parameter: https://your-function-url.net/render?requestURL=https://yahoo.com
export const render = functions |
And that’s all there is to it. You now have access to all the awesomeness of Puppeteer in a Firebase Cloud Function.