VOOZH about

URL: https://thenewstack.io/how-to-build-a-carbon-aware-website-using-react-and-next-js/

⇱ How To Build a Carbon-Aware Website Using React and Next.js - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2024-10-18 09:00:47
How To Build a Carbon-Aware Website Using React and Next.js
tutorial,
Frontend Development / JavaScript

How To Build a Carbon-Aware Website Using React and Next.js

You can make a carbon-aware website in three simple steps, by harnessing the power of server-side rendering on Next.js.
Oct 18th, 2024 9:00am by Dryden Williams
👁 Featued image for: How To Build a Carbon-Aware Website Using React and Next.js
Image via Unsplash+. 
Most people browsing the web are unaware that every click, scroll, and page view contributes to environmental emissions. The internet, while essential to our daily lives, consumes massive amounts of electricity, much of which is sourced from fossil fuels. As web developers, we have an opportunity to reduce this impact through an exciting new concept called a carbon-aware website. A carbon-aware website is one that changes its features based on the grid intensity of the electricity grid. In this article, we’re going to look at what a carbon-aware website is and how it behaves, and why it’s both good for the user and for the planet. For further inspiration, check out Low-tech Magazine, a solar-powered website I’ve been fascinated with for some time. Branch and Fershad have also implemented carbon-aware sites.

Grid and Carbon Intensity for Web Developers

Carbon intensity refers to the amount of carbon dioxide (CO₂) emitted to produce a kilowatt-hour (kWh) of electricity. It varies significantly by region, depending on the sources of power available. A low carbon intensity is considered to be below 50 grams of CO₂ per kWh, while coal-reliant countries, like Poland, can reach levels as high as 600 grams per kWh. Different countries produce different amounts of electricity from different sources. Some of it is green and renewable (low intensity) and some is produced using fossil fuels (high intensity). Websites use electricity in data centres, telecoms networks and end user devices. In regions with abundant renewable energy — such as Norway, Sweden, and Iceland, where carbon intensity remains low due to reliance on hydropower and wind energy — the emissions of viewing websites and interacting with the web is low. When grid intensity is high, meaning that most electricity is generated from fossil fuels, a carbon-aware website will switch to a “low-carbon” mode, reducing the amount of energy required for users to access it. In contrast, when the grid is powered by cleaner, renewable sources, the website can deliver a richer experience with more features. This approach reduces the website’s carbon footprint.

Why Carbon Awareness Matters in Web Design

Did you know that approximately 54% of a website’s carbon emissions come from the end-user’s device? That’s a significant portion of a site’s impact that can be managed by adjusting the experience based on grid conditions.

Make a Carbon-Aware Website Using React and Next.js

We can make a carbon-aware website in three simple steps, by harnessing the power of server-side rendering on Next.js. Assuming you’ve installed Next.js with the App Router, firstly signup at carbonaware.cloud to get your API keys. It’s a free service and we’re going to be using the requestInfo endpoint. Secondly, we’ll create a server-side function to fetch the live grid data, using the code below. We use the CarbonAwareAPI for this. It’s nice and clever and will do the IP fetch from our request headers, so that we can find the client’s country code (we need the country code to find the current live grid intensity). Once we have that, we can pass it back to our components on the front end.
// src/app/actions.ts


"use server";


import { headers } from "next/headers";


import CarbonAwareAPI, {
 pickHeaders,
 allowedHeaders,
} from "@fikastudio/carbonaware";

// Get API key from https://carbonaware.cloud/
const client = new CarbonAwareAPI(
 process.env.CARBONAWARE_ACCESS_TOKEN!,
 process.env.CARBONAWARE_SECRET_TOKEN!
);


export const fetchCarbonIntensity = async (): Promise<number> => {
 try {
 // Fetch the headers for the IP/Client country code
 const hdrs = await headers();

 // The CarbonAwareAPI is clever and will get the client country from this
 // This will return a live grid intensity for the client
 const resp = await client.requestInfo({
 headers: pickHeaders(hdrs, allowedHeaders.fly),
 remoteAddr: hdrs.get("fly-client-ip") || ''
 });


 return resp.intensity
 } catch (error) {
 // Default back to the global average
 return resp.globalIntensity;
 }
};
Step 3 is to fetch the grid data from our action, in order to dynamically change the page content.
// /src/app/page.tsx
export default async function Home() {
 const intensity = await fetchCarbonIntensity();

const isLowIntensity = intensity < 100
const isMedIntensity = intensity > 100 && intensity < 300


 if (isLowIntensity) {
 return (
 <div>
	<h1>Low intensity view</h1>
<img src="my-eco-hosting.jpg" alt="hi-intensity hi-res image" width="1000" />
	</div>
 );
 }


 if (isMedIntensity) {
 return 
 <div>
	<h1>Medium intensity view</h1>
<img src="my-eco-hosting.jpg" alt="med-res image" width="1000" />
	</div>
 }


 return (
 <div>
	<h1>High intensity view</h1>
<p>No image to show</p>
	</div>
)
}
By dynamically changing the content based on the live grid data, we’ve made this website carbon-aware! In the output we show a hi-res image if the intensity is low, a medium-resolution image if it’s medium, and we’ve decided not to show an image at all if the grid intensity is high (we just display text). There are so many fun options we could do with this technique, such as:
  • Change theme: We can change the site colours.
  • Minimize Features: Cut down on non-essential content and animations, focusing only on core information.
  • Use System Fonts: Replace custom fonts with system defaults, reducing data transfer and load times.
  • Optimize Images or Show Text Alternatives: Reduce image quality, serve lower-resolution images, or replace images with text-based alternatives to save bandwidth.
  • Video: Remove the video and show text if the grid is too high. If the grid has lots of renewables, feel free to add an auto-play video (if you need it).
  • Carbon budgets: We can limit the amount of content shown on the site based on grid intensity. The more renewable, the more content the user can view.
Conversely, if renewable sources power the grid, a carbon-aware site might offer an enriched experience — high-resolution images, autoplay videos, and custom fonts that enhance the visual appeal.

Conclusion

By changing a website’s features based on the electricity grid’s carbon intensity at any given time, we can reduce overall page transfer and the impact on the end users’ devices — providing them with a richer experience. It saves their data plans, loads a faster website, and is much better for the planet.
TRENDING STORIES
Dryden is a sustainable web consultant building EcoPing.earth. He is helping companies measure and reduce their website carbon emissions.
Read more from Dryden Williams
SHARE THIS STORY
TRENDING STORIES
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.