diff --git a/lib/plugins/sustainable/index.js b/lib/plugins/sustainable/index.js index 71b41fadef..1f664ea092 100644 --- a/lib/plugins/sustainable/index.js +++ b/lib/plugins/sustainable/index.js @@ -79,7 +79,22 @@ export default class SustainablePlugin extends SitespeedioPlugin { DEFAULT_METRICS_PAGE_SUMMARY, 'sustainable.pageSummary' ); + + const greenDomainJSONpath = path.join( + __dirname, + 'data', + 'url2green.json.gz' + ); + + if ( + !this.sustainableOptions.disableHosting && + !this.sustainableOptions.useGreenWebHostingAPI + ) { + log.info('Reading green domain data from disk'); + this.data = await loadJSON(greenDomainJSONpath); + } } + async processMessage(message, queue) { const make = this.make; const aggregator = this.aggregator; @@ -118,23 +133,21 @@ export default class SustainablePlugin extends SitespeedioPlugin { case 'pagexray.run': { // We got data for a URL, lets calculate co2, check green servers etc const listOfDomains = Object.keys(message.data.domains); - const greenDomainJSONpath = path.join( - __dirname, - 'data', - 'url2green.json.gz' - ); let hostingGreenCheck; if (this.sustainableOptions.disableHosting === true) { hostingGreenCheck = []; } else { - hostingGreenCheck = - this.sustainableOptions.useGreenWebHostingAPI === true - ? await hosting.check(listOfDomains, undefined, 'sitespeed.io') - : await hosting.check( - listOfDomains, - await loadJSON(greenDomainJSONpath) - ); + if (this.sustainableOptions.useGreenWebHostingAPI === true) { + log.info('Fetch hosting information from The Green Web Foundation'); + hostingGreenCheck = await hosting.check( + listOfDomains, + undefined, + 'sitespeed.io' + ); + } else { + hostingGreenCheck = await hosting.check(listOfDomains, this.data); + } } const configuration = { model: this.sustainableOptions.model };