This repo is a demo website for the Green Web Foundation's Grid-aware Websites project. It demonstrates grid-aware code being run on routes using Cloudflare Workers.
You can see this code working on the public internet at: https://gaw.greenweb.org
This demo shows the @greenweb/grid-aware-websites
package being used to check the state of the client's energy grid in order to determine if changes should be made to the returned web page. The code runs in two Cloudflare Workers, one for each of the routes below:
/power-breakdown
: This worker checks to see if the current percentage of renewable energy being used on the client's electricity grid is below a set threshold (in our case 50%). If it is, then the@greenweb/grid-aware-websites
library returns a value indicating that changes should be made to the returned web page to reduce the energy it might use on the client's device. These changes are applied using theHTMLRewriter
API. See the Worker source code../grid-breakdown
: This worker checks to see if the current grid carbon intensity (in grams CO2e/kilowatt-hour) of the client's electricity grid is above the last known annual average. If it is, then the@greenweb/grid-aware-websites
library returns a value indicating that changes should be made to the returned web page to reduce the energy it might use on the client's device. These changes are applied using theHTMLRewriter
API. See the Worker source code..
For more information, see the Grid-aware Websites project repository.
The example
folder contains a boilerplate Cloudflare Worker script that can help you get started. To use it:
- Copy the files to a folder on your computer.
- In your terminal, navigate to that folder and run the
npm install
command. - Rename the
example.dev.vars
to.dev.vars
. Inside that file, update theEMAPS_API_KEY
variable to your Electricity Maps API Key. - Modify the
src/index.js
file as desired. It is setup to use thegridAwarePower
function from the@greenweb/grid-aware-websites
library. - Update the
wrangler.toml
file with a project name, and point it to the routes you want to deploy this Worker on. - Test the worker by running
npx wrangler dev
. Deploy the Worker by runningnpx wrangler deploy
.
This website is a minimal Eleventy site with some static assets. To run this project locally, you can:
- Clone the repository
- In the root folder for this project, run
npm install
. - In the
workers/co2e
andworkers/power
folders, rename theexample.dev.vars
to.dev.vars
. Inside that file, update theEMAPS_API_KEY
variable to your Electricity Maps API Key. - To run the site locally, you can run the following commands:
npm run dev:power
to check the/power-breakdown
route.npm run dev:co2e
to check the/grid-intensity
route.
You can deploy this site to Cloudflare Pages by following their regular deployment instructions for Eleventy websites.
Before deploying the Cloudflare Workers, you should first change the routes
in the wrangler.toml
files that are located in the workers/co2e
and workers/power
folders. Change the routes to the url pattern
& zone_name
or zone_id
your recently deployed site.
To deploy the Cloudflare Worker scripts, run the following command in the root of this project.
npx wrangler login
. This will log you in to your Cloudflare account.npm run workers:deploy
. This should deploy the two workers to the Cloudflare zone you've specified.