I have a PHP app that needs to take some screenshots. Currently it spawns then destroys a Puppeteer/Chrome instance each time, but I want to re-use the instance to increase performance when it runs batches.
In pseudo code: script 1: launch Puppeteer, grab the instance URL/websocket address, disconnect; script2: loop over screenshots; script 3: close
My JS is letting me down I think – when it hits browser.disconnect();
it never returns to PHP as the process doesn’t complete.
—
Example code:
PHP: function spawn_chrome() { exec( 'node spawn_chrome.js', $command_output, $command_result ); [...] };
JS (spawn_chrome.js):
const puppeteer = require('/usr/local/lib/nodejs/node_modules/puppeteer');
(async () => {
const browser = await puppeteer.launch({
headless: 'shell',
executablePath: '/usr/bin/chromium',
args: [
'--no-sandbox',
[...]
'--use-mock-keychain',
]
});
var browserWSEndpoint = browser.wsEndpoint();
await browser.disconnect();
console.log(browserWSEndpoint);
})();
If I replace await browser.disconnect();
with await browser.close();
the script finishes/returns. But according to the docs they both return a promise so I’m surprised they’re behaving differently.
Is my JS failing here? Or have I misunderstood it more fundamentally and you can’t disconnect and finish script processing while leaving the Chrome instance up?