Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
67 changes: 67 additions & 0 deletions puppeteer-browser-pool.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* 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 };
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, we need linting.

  • Can you remove the surrounding whitespaces after { and the first key name and the space before }.
  • Use 2-space indentation everywhere.

const HACKER_NEWS_SPA_URL = 'https://hn.algolia.com/';
const HACKER_NEWS_QUERY_SPA_URL = 'https://hn.algolia.com/?query=angular';
Copy link
Contributor

Choose a reason for hiding this comment

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

let's use https://hn.algolia.com/?query=chrome

Copy link
Contributor

Choose a reason for hiding this comment

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

This could also build off the base url.



const factory = {
Copy link
Contributor

Choose a reason for hiding this comment

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

can we use a better name?

Choose a reason for hiding this comment

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

no

create: async () => {
const browser = await puppeteer.launch(puppeteerArgs);
Copy link
Contributor

Choose a reason for hiding this comment

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

where does puppeteerArgs come from?

const page = await browser.newPage();
return page;
},
destroy: (browser) => {
browser.close();
Copy link
Contributor

Choose a reason for hiding this comment

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

return browser.close();

},
};

const browserPool = genericPool.createPool(factory, {
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_SPA_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_SPA_URL);
console.log(responseHackerNewsWithQuery);
})();
Copy link
Contributor

Choose a reason for hiding this comment

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

newline

Loading