The concept of a carbon-aware website is one where the content/user experience of a site changes depending on how the grid intensity of the electricity grid. Implementing carbon awareness on this website was inspired by the work of Branch Magazine.
This repository contains a Cloudflare Workers starter template that can be used to make a website carbon-aware. It is a very bare bones implementation, and is designed to serve as a launching pad, from which developers can begin to play around with the idea of carbon awareness on their own sites or apps.
You can read more about this at: https://fershad.com/carbon-aware-site/
To start working with the code, clone this repository & run npm install
in your console.
- A Cloudflare account with Workers functionality enabled.
- A CO2signal API key. Note that CO2signals does have rate limits. If your site receives lots of traffic, then you should consider using Electricity Maps paid API.
All code for this project lives in the src/index.js
file. I've left comments in there so that you can hopefully get a grips of how everything works.
You can deploy this Cloudflare Worker using the Wrangler CLI. Run wrangler publish
and follow the prompts.
To have the worker execute on specific page routes, you should update the wrangler.toml
file to include a route
key.