Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add example for pooling browsers #14

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"cli-table": "^0.3.1",
"del": "^3.0.0",
"express": "^4.16.3",
"generic-pool": "^3.4.2",
"lighthouse": "^2.9.4",
"mime": "^2.3.1",
"node-fetch": "^2.1.2",
Expand Down
66 changes: 66 additions & 0 deletions puppeteer-browser-pool.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* Copyright 2018 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @author rahulkumar66@ (Rahul Kumar)
*/

/**
* Demonstrates how to reuse browser instances across multiple tasks
* resulting in improved load times for subsequent requests.
* This has huge load time improvements in case of SPAs
*/

const genericPool = require('generic-pool');
const puppeteer = require('puppeteer');

const VIEWPORT = {width: 1028, height: 800, deviceScaleFactor: 2};
const HACKER_NEWS_URL = 'https://hn.algolia.com/';
const HACKER_NEWS_QUERY_URL = `${HACKER_NEWS_URL}?query=chrome`;

const browserPoolFactory = {
create: async () => {
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
return page;
},
destroy: (browser) => {
return browser.close();
Copy link

@SebastiyanKasurov SebastiyanKasurov Nov 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You receive page from (create), in destroy you receive bro page, so page will be closed not browser

},
};

const browserPool = genericPool.createPool(browserPoolFactory, {
max: 5, // maximum number of browser instances that can be created at any given time
min: 2, // minimum number of browser instances to keep in pool at any given time
maxWaitingClients: 50
});

async function getContent(url) {
const page = await browserPool.acquire();
await page.setViewport(VIEWPORT);

const response = await page.goto(url, {waitUntil: 'networkidle2', timeout: 60000});
// release browser instance back to pool so it can be reused
await browserPool.release(page);
return response.text();
}

(async () => {
const responseHackerNews = await getContent(HACKER_NEWS_URL);
console.log(responseHackerNews);

// this request will use the instance from the current browser pool resulting in faster response
const responseHackerNewsWithQuery = await getContent(HACKER_NEWS_QUERY_URL);
console.log(responseHackerNewsWithQuery);
})();
Loading