Click for high-res image.

Release guide: CO2.js v0.10

The v0.10.0 release of CO2.js saw several large developer experience and usability improvements added to the library. Although, at the time of writing, the v0.10.x releases have already been out for over a month, we feel that it is important to document and cover some of the key updates delivered in this version of the library.

CO2.js goes hybrid

The v0.10.1 release introduced an updated build process for the CO2.js library, using esbuild. This allowed for three different versions of the library to be generated, to target three kinds of platforms:

  1. A browser build using an IIFE
  2. A CommonJS compatible build for pre-ESM versions of NodeJS
  3. An ES Modules compatible build for modern, ESM compatible JavaScript runtimes

By making this change, we enable developers to easily use CO2.js in more places. Examples of this new capability includes running CO2.js in a Cloudflare Worker, and calculating emissions in the browser.

Streamline changing between estimation models

In v0.10.2 of CO2.js we simplified the steps required to switch between the OneByte and Sustainable Web Design carbon estimation models. Previously, developers wishing to switch from the default OneByte model to the newer Sustainable Web Design model had to manually change the CO2.js source code.

v0.10.2 introduced a new model parameter which can be passed to CO2.js as an option. By passing in { model: "swd" }, developers can quickly switch to the Sustainable Web Design model after importing the library into their project.

The addition of this parameter also makes it possible for us to change the default model while still providing a frictionless way for developers to continue using the OneByte model if they wish to.

You can find more details, and code examples, for changing models in the CO2.js docs.

Adding a perVisit method

CO2.js has always had a perByte method, which returns a CO2 value (in grams) for raw data transfer. In v0.10.3, we introduced a new perVisit method which can be used specifically for calculating the carbon emissions of websites.

The perVisit method is only available when using the Sustainable Web Design model. It adopts the assumptions the model authors have made about website visitors and caching as part its calculation.

You can find more details about both models, and code samples using both, in the CO2.js docs.

You can find details of every release for CO2.js on GitHub, where you’ll also be able find the changelog for this project.

If you are using CO2.js in production then The Green Web Foundation would love to hear from you! Use the contact form on the website to get in touch.