From 279120dd039498da450fefbb57bbc5a11763fc44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20S=C3=A1nchez?= Date: Mon, 14 Sep 2020 09:38:48 +0200 Subject: [PATCH] Launch puppeteer browser on app start --- print/boot.js | 16 +++++++++++++++- print/core/report.js | 8 +------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/print/boot.js b/print/boot.js index 02bb27817..5e84bb5dd 100644 --- a/print/boot.js +++ b/print/boot.js @@ -1,11 +1,13 @@ const express = require('express'); const path = require('path'); const fs = require('fs'); +const puppeteer = require('puppeteer'); const templatesPath = path.resolve(__dirname, './templates'); const componentsPath = path.resolve(__dirname, './core/components'); +const config = require('./core/config'); -module.exports = app => { +module.exports = async app => { global.appPath = __dirname; process.env.OPENSSL_CONF = '/etc/ssl/'; @@ -51,4 +53,16 @@ module.exports = app => { app.use(`/api/${templateName}/assets`, express.static(assetsDir)); }); }); + + // Instantiate Puppeteer browser + async function launchBrowser() { + config.browser = await puppeteer.launch({ + headless: true, + args: ['--no-sandbox', '--disable-setuid-sandbox'] + }); + + config.browser.on('disconnected', launchBrowser); + } + + launchBrowser(); }; diff --git a/print/core/report.js b/print/core/report.js index c5847fda1..46404ff96 100644 --- a/print/core/report.js +++ b/print/core/report.js @@ -1,5 +1,4 @@ const fs = require('fs'); -const puppeteer = require('puppeteer'); const path = require('path'); const config = require('./config'); const Component = require('./component'); @@ -28,11 +27,7 @@ class Report extends Component { if (fs.existsSync(fullPath)) options = require(optionsPath); - const browser = await puppeteer.launch({ - headless: true, - args: ['--no-sandbox', '--disable-setuid-sandbox'] - }); - const page = await browser.newPage(); + const page = await config.browser.newPage(); await page.emulateMedia('screen'); await page.setContent(template); @@ -51,7 +46,6 @@ class Report extends Component { options.footerTemplate = footer; const buffer = await page.pdf(options); - await browser.close(); return buffer; }