I have seen others having relatively minor performance problems with puppeteer running on Firebase Functions. In my case, Firebase times out before I can do anything with puppeteer, even with the memory and timeoutSeconds cranked all the way up.
Code:
JavaScript
x
54
54
1
import * as functions from "firebase-functions";
2
import puppeteer from "puppeteer";
3
4
const runScreenshot = async () => {
5
console.log("Launching puppeteer...");
6
console.time("launch");
7
const browser = await puppeteer.launch();
8
console.timeEnd("launch");
9
10
console.log("Awaiting browser.newPage()...");
11
console.time("newPage");
12
const page = await browser.newPage();
13
console.timeEnd("newPage");
14
15
console.log("Setting viewport...");
16
console.time("setViewport");
17
page.setViewport({ width: 2000, height: 4000, deviceScaleFactor: 4 });
18
console.timeEnd("setViewport");
19
20
console.log("Goto page...");
21
console.time("goto");
22
await page.goto(
23
"https://public.tableau.com/views/NCDHHS_COVID-19_Dashboard_Summary/NCDHHS_DASHBOARD_SUMMARY"
24
);
25
console.timeEnd("goto");
26
27
console.log("Waiting for selector...");
28
console.time("selector");
29
await page.waitForSelector("#tab-dashboard-region");
30
console.timeEnd("selector");
31
32
// Wait for the spinner to go away
33
await new Promise((r) => setTimeout(r, 100));
34
35
console.time("href");
36
const href = await page.$("#tab-dashboard-region");
37
console.timeEnd("href");
38
39
console.time("screenshot");
40
href && (await href.screenshot({ path: "tmp/test.png" }));
41
console.timeEnd("screenshot");
42
43
return browser.close();
44
};
45
46
export const screenshot = functions
47
.runWith({ memory: "2GB", timeoutSeconds: 540 })
48
.pubsub.schedule("44 21 * * *")
49
.timeZone("America/New_York")
50
.onRun(() => {
51
return runScreenshot();
52
});
53
54
Here’s the resulting Firebase Functions log. It takes a few seconds to run puppeteer.launch()
, and then browser.newPage()
won’t finish at all in the 8 minutes it has before the timeout.
JavaScript
1
16
16
1
9:44:04.012 PM
2
screenshot
3
Function execution started
4
9:44:04.555 PM
5
screenshot
6
Launching puppeteer
7
9:44:09.427 PM
8
screenshot
9
launch: 4870.195ms
10
9:44:09.427 PM
11
screenshot
12
Awaiting browser.newPage()
13
9:53:04.014 PM
14
screenshot
15
Function execution took 540004 ms, finished with status: 'timeout'
16
Complete reproducible example: https://github.com/danbockapps/firebase-puppeteer
Advertisement
Answer
I fixed this by adding {args: [ '--no-sandbox', '--disable-setuid-sandbox']}
to puppeteer.launch, as recommended here: https://github.com/puppeteer/puppeteer/issues/6568