// Take control of the browser instance used by Penthouse.
// Can be used to setup custom configuration,
// to avoid browser open/close calls for improved performance, and more.
// See puppeteer docs for more options:
// https://github.com/GoogleChrome/puppeteer).

import penthouse from 'penthouse'
import puppeteer from 'puppeteer' // installed by penthouse

const browserPromise = puppeteer.launch({
  ignoreHTTPSErrors: true,
  args: ['--disable-setuid-sandbox', '--no-sandbox']
})
penthouse({
  url: 'http://google.com',
  cssString: 'body { color: red }',
  puppeteer: {
    getBrowser: () => browserPromise
  }
})
.then(criticalCss => {
  // _after_ you are done with _all_ of your Penthouse calls
  // - NOT just this single one -
  // close the browser you started:
  browserPromise.then(browser => browser.close())
})

// Take control of the browser instance used by Penthouse.
// Can be used to setup custom configuration, and more.
// See puppeteer docs for more options:
// https://github.com/GoogleChrome/puppeteer).
import penthouse from 'penthouse'
import puppeteer from 'puppeteer' // installed by penthouse

const browserPromise = puppeteer.launch({
  ignoreHTTPSErrors: true,
  args: ['--disable-setuid-sandbox', '--no-sandbox']
})
penthouse({
  url: 'http://google.com',
  cssString: 'body { color: red }',
  puppeteer: {
    getBrowser: () => browserPromise
  }
})
.then(criticalCss => {
  // use it
})

// NOTE: by default Penthouse closes the browser it uses as soon as there are no ongoing jobs,
// even when a custom `getBrowser` function is used. You can currently override this behavior
// (f.e. to avoid open/close browser) via:
// `unstableKeepBrowserAlive` prop - which as the name suggests is subject to change in the future.
// If you do you have to call ~ `browserPromise.close()` yourself to avoid memory leaks!

Clarify browser is closed even with getBrowser option
Clarify browser is closed even with getBrowser option

Avoid extra burden of having to close browser, but also document how it can be kept alive.
Possible that should switch in future to shift responsibility of closing custom browser to user.