LittleBigThing / carbon-footprint

A WordPress plugin to spread awareness about the carbon footprint of websites and to help making WordPress sites become more sustainable. The information is based on data from the Website Carbon Calculator.
GNU General Public License v2.0
5 stars 0 forks source link

Refactoring the request to decrease load on Website Carbon API #7

Open LittleBigThing opened 1 year ago

LittleBigThing commented 1 year ago

Currently, the plugin is using the Website Carbon (WC) API's site endpoint to test what the homepage's carbon footprint is and how it compares to other pages tested, and to test whether the site uses renewable energy.

This may give a high load on the API if the plugin gets adopted on a larger scale. Due to limitations to the number of requests for the API and the possibility to do this differently, it is suggested to refactor the way the test results are collected.

The one request to the WC API could be replaced by

  1. a request to The Green Web Foundation's (TGWF) API to determine whether the site runs on renewable energy
  2. a request to the PageSpeed Insights (PSI) API to determine the size of the homepage (total byte weight)
  3. use the result of 2 (and 1, although not strictly necessary) to send a request to the WC API's data endpoint, which requires the page size in bytes, to determine the homepage's carbon footprint and how it compares to other pages tested

This might seem to make it more complex (and it is more complex code-wise), but this is what happens in the background anyway using the WC API.

There are also a couple of advantages:

A disadvantage is the large amount of data provided by the PSI API. This is also the most time consuming. On the other hand, other useful information could be cached from this API and we could provide sustainability-related useful information from it as well. In how I interpret it, we do not need an API key to use this service.

If there is any way to get the 'total byte weight' of a web page in any other way, please let me know. That is the most tricky step in the process.