Lighthouse is an open-source tool which is used to generate performance of any web page. We can use CLI or chrome extension to generate lighthouse score for a web page but here is the description of how to generate lighthouse score of multiple urls present in a csv file and generate a csv file consisting of scores of Performance, Accessibility, Best-Practices and SEO for both Mobile and Desktop views.
We use the lighthouse package provided by npm and use the chrome-launcher as well to generate scores of web sites. We can download the package and use them as follows:
npm install --save lighthouse
const lighthouse = require("lighthouse")
After importing this third party package, we are ready to proceed further to import the URL file by using readFileSync method of the default fs package provided by NodeJS and store these URLs in an array from which we will iterate through all the URLs and generate scores for both Mobile and Desktop strategies.
Follow the steps below to achieve the solution:
- Read the csv file using default fs npm package.
- Convert the data to String and split it in an array.
- Push the header columns in the array.
- Launch the chrome using chromeLauncher.launch() method.
- Create an options object to specify the details of output format, categories, audits and other customization if any you want to have in your output.
- Traverse through all the urls we have in our array and do the following for each one of it:
- Run two loops: one for mobile and other for desktop scores by specifying options.strategy=mobile or options.strategy=desktop.
- Call the lighthouse function with url i.e. array[i] as the first parameter and options object as the second parameter.
- The object returned would store the details received after calling the lighthouse function.
- We can access the scores from this object and since the scores are in decimals, we need to multiply it with 100 to get it as percentage.
- Push the scores in the result.
- Append the result in a csv file and end the program.
- Nodejs | Automatic restart NodeJs server with nodemon
- Generating Random String Using PHP
- Dynamically generating a QR code using PHP
- Generating Errors using HTTP-errors module in Node.js
- Nodejs - Connect MongoDB with Node app using MongooseJS
- Cowsay in Nodejs using Requests library
- Nodejs | Web Crawling using Cheerio
- Signup Form Using Nodejs and MongoDB
- Nodejs | authentication using Passportjs and passport-local-mongoose
- NodeJs CRUD Operations Using Mongoose and MongoDB Atlas
- Web-Socket in NodeJS
- Dockerizing a simple Nodejs app
- Nodejs | DNS
- Encrypting Data in NodeJS
- Difference between NodeJS and AngularJS
- Nodejs | Jimp
- Nodejs – Connect Mysql with Node app
- Nodejs | DNS | setServers()
- NodeJS | Building simple REST API in express
- NodeJs - Handling invalid routes
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.