feat(sustainable) : added countryCode option for country specific CO₂ calculations#4583
feat(sustainable) : added countryCode option for country specific CO₂ calculations#4583Harsh-Git39 wants to merge 6 commits intositespeedio:mainfrom
Conversation
|
Hi @Harsh-Git39 ah cool, I'll have a look and test it this weekend! |
|
Hi @soulgalore! I've fixed all the linting errors. The CRUX test is failing due to a missing API key in the CI configuration (--crux.key is empty), which appears to be unrelated to my changes. All other tests are passed . Could you please review? |
|
Hi @soulgalore is this solution as expected or I'm happy to make further corrections if needed. |
soulgalore
left a comment
There was a problem hiding this comment.
What about also showing on the result page which country code that was used? That makes it easier to remember what you actually used.
I haven't tried the actual functionality on c02, so if I just switch between two different country codes I can verify that it works?
| example:- | ||
| ```bash | ||
| sitespeed.io https://example.com \ | ||
| --plugins.add sustainable \ |
There was a problem hiding this comment.
In the rest of the documentation we use --sustainable.enable, I think we should use that here too.
There was a problem hiding this comment.
makes sense I can update the result page to also show which countryCode was used, so it’s clear what configuration the CO₂ calculation is based on and for documentation I will make a update shortly.
| super({ name: 'sustainable', options, context, queue }); | ||
| } | ||
|
|
||
| static getCliOptions() { |
There was a problem hiding this comment.
Does this work? I think the options for now could be moved to lib/cli/cli.js where the rest of the sustainable options is today, to keep it consistent.
There was a problem hiding this comment.
It works I have tested it locally switching between two different country codes shows different result in different CO2 values and I will move and test it on cli.js to ensure consistency and get back to you shorty
Makes users to calculate CO₂ emissions using a country-specific electricity grid mix via electricitymaps (through
@tgwf/co2).Implementation
Added
sustainable.countryCodeas a plugin CLI option.Passed the value into the CO₂ model configuration when creating the
co2instance.Updated the sustainable plugin documentation with a implementation example .
Testing and observations
I tested it with different country codes (IN, DE, NO). On very small pages (example.com) the difference is tiny due to rounding, but on larger pages like wikipedia.org the CO₂ values do change (for example, India ≈ 0.03730 vs Norway ≈ 0.03710), which shows the country-specific electricity mix is being used.
On lightweight pages (like example.com / wikipedia), the differences in CO₂ values are small, but they are consistently visible across runs, which suggests the country-specific electricity mix is being applied correctly. Heavier pages should make the differences more noticeable.
Thanks a lot for opportunity!!