
How to Include Open Graph in a React Application for Your E-commerce Website
How to Include Open Graph in a React Application for Your E-commerce Website
If you’ve ever shared a product link from an e-commerce site on social media and noticed a rich preview with images, titles, and descriptions, you’ve encountered Open Graph meta tags in action. These tags play a crucial role in how your content is displayed on platforms like Facebook, Twitter, LinkedIn, and WhatsApp. For e-commerce businesses, this is not just a nice-to-have feature—it can significantly impact click-through rates, brand perception, and ultimately conversions.
Why Open Graph Matters for Your React E-commerce Site
React, being a client-side JavaScript framework, doesn’t render meta tags on the server by default, which poses a challenge for SEO and social media crawlers that rely on server-rendered HTML. When a social platform’s bot fetches your page, it looks for Open Graph tags in the HTML header. Without them, you risk having no preview or a generic, unappealing one.
Imagine you’ve just launched a new sneaker model and want users sharing the link on Facebook to see a stunning image, the product name, and a compelling description. Proper Open Graph integration ensures your product page looks polished and professional everywhere.
Understanding Open Graph Meta Tags
Open Graph protocol was introduced by Facebook to standardize the way web pages represent their content when shared socially. Here are the core OG tags you should know:
Tag | Purpose | Example |
---|---|---|
og:title | The title of your content | "Nike Air Zoom Pegasus 38" |
og:description | A brief description of the content | "Experience ultimate comfort and performance." |
og:image | URL of the image representing the content | "https://example.com/images/shoe.jpg" |
og:url | Canonical URL of the page | "https://example.com/product/1234" |
og:type | Type of object (e.g., website, product) | "product" |
Other optional tags like og:site_name
and og:locale
can further enrich the preview.
Challenges of Adding Open Graph in React Apps
React apps are typically single-page applications (SPAs), meaning the HTML served initially is minimal and meta tags are dynamically injected by JavaScript after the page loads. Social media crawlers usually don't execute JavaScript, so they miss these dynamic tags.
How to overcome this?
- Server-Side Rendering (SSR) or Static Site Generation (SSG): Frameworks like Next.js render your React components on the server, sending fully formed HTML with meta tags.
- Pre-rendering: Use tools that generate static HTML snapshots.
- Dynamic meta tags injection on the server: Use Node.js or middleware to inject appropriate meta tags before sending the response.
- Third-party services: Some services can scrape your SPA and serve meta tags.
If your app uses Create React App (CRA) or a client-only setup, you’ll need to adopt one of these strategies for Open Graph to work effectively.
Step-by-Step Tutorial: Adding Open Graph Tags in a React E-commerce Site
Let’s explore a practical example using Next.js, a popular React framework that supports SSR out of the box. This approach fits perfectly for an e-commerce site needing dynamic meta tags per product page.
1. Setting Up Next.js
If you don't have a Next.js project yet, start one:
npx create-next-app@latest ecommerce-og-demo
cd ecommerce-og-demo
npm run dev
2. Create a Dynamic Product Page
Inside the pages
directory, create [id].js
to handle dynamic product routes:
import Head from 'next/head';
const products = {
'1234': {
title: 'Nike Air Zoom Pegasus 38',
description: 'Experience ultimate comfort and performance.',
image: 'https://example.com/images/shoe.jpg',
url: 'https://yourdomain.com/product/1234',
},
'5678': {
title: 'Adidas Ultraboost 21',
description: 'Boost your runs with unparalleled energy return.',
image: 'https://example.com/images/adidas.jpg',
url: 'https://yourdomain.com/product/5678',
},
};
export default function ProductPage({ product }) {
if (!product) return <p>Product not found</p>;
return (
<>
<Head>
<title>{product.title}</title>
{/* Open Graph meta tags */}
<meta property="og:title" content={product.title} />
<meta property="og:description" content={product.description} />
<meta property="og:image" content={product.image} />
<meta property="og:url" content={product.url} />
<meta property="og:type" content="product" />
<meta name="twitter:card" content="summary_large_image" />
</Head>
<main>
<h1>{product.title}</h1>
<img src={product.image} alt={product.title} width={400} />
<p>{product.description}</p>
</main>
</>
);
}
export async function getServerSideProps(context) {
const { id } = context.params;
const product = products[id] || null;
return {
props: { product },
};
}
Explanation:
- We use Next.js’s
Head
component to insert meta tags into the<head>
. - The product data is fetched on the server side using
getServerSideProps
, ensuring the HTML sent to the browser (and crawlers) contains the appropriate Open Graph tags. - This way, when someone shares
https://yourdomain.com/product/1234
, social platforms get all the relevant metadata to generate a rich preview.
3. Testing Your Open Graph Tags
Once deployed (or running locally with a public tunnel like ngrok), test your pages with:
These tools show exactly what metadata each platform reads, helping you verify your implementation.
Alternative: Using React Helmet for Client-Side Meta Tags
If your app is client-rendered and SSR is not an option, you can use react-helmet
to manage meta tags dynamically. However, remember that most social media bots won’t execute your JavaScript, so the tags might not be picked up reliably.
Example:
import { Helmet } from 'react-helmet';
function Product({ product }) {
return (
<>
<Helmet>
<title>{product.title}</title>
<meta property="og:title" content={product.title} />
<meta property="og:description" content={product.description} />
<meta property="og:image" content={product.image} />
<meta property="og:url" content={product.url} />
<meta property="og:type" content="product" />
</Helmet>
<h1>{product.title}</h1>
<img src={product.image} alt={product.title} />
<p>{product.description}</p>
</>
);
}
Use this method with caution and preferably combine it with pre-rendering or SSR for production e-commerce sites.
Best Practices for Open Graph on E-commerce Sites
- Use high-resolution images for
og:image
(minimum 1200x630px) to ensure crisp previews. - Keep descriptions concise and persuasive, around 2-4 sentences.
- Set
og:type
toproduct
to help social platforms understand the content. - Provide unique meta tags per product page, avoiding duplication.
- Include Twitter Card tags like
twitter:card
for Twitter-specific previews. - Test regularly after deploying changes to confirm tags are recognized.
Final Thoughts
Integrating Open Graph tags in your React e-commerce application is not just a technical detail—it's a strategic step to enhance your brand’s social presence and boost traffic. While React’s SPA nature complicates SEO and metadata, leveraging frameworks like Next.js with SSR or SSG provides a clean, scalable solution.
What’s particularly interesting is how this small set of meta tags can transform the way your products appear across platforms, enticing users with rich visuals and compelling descriptions. And in the competitive world of e-commerce, every bit of engagement counts.
If you’re still running a purely client-side React app, consider adopting SSR or pre-rendering soon. Your social media previews—and your bottom line—will thank you.
“Open Graph meta tags might seem trivial at first glance, but they serve as your product’s handshake in the social world—make sure it’s firm, welcoming, and memorable.”
Further Reading & Resources
- Open Graph Protocol Official Site
- Next.js Documentation on SEO
- Facebook Sharing Debugger
- React Helmet GitHub
- How to Implement SEO in React
If you found this guide helpful, stay tuned for more insights into React, SEO, and e-commerce development strategies!
— Monil Prajapati
Join Our Newsletter
Get the latest articles, tutorials, and insights delivered straight to your inbox. Join thousands of developers staying ahead of the curve.
Related Articles
Continue reading with these hand-picked articles

Redux vs Zustand: A Comprehensive Comparison of React State Management Libraries
Explore a detailed comparison between Redux and Zustand, two popular React state management libraries. Learn their philosophies, setup, performance, use cases, and practical examples to choose the best tool for your next project.

Building Progressive Web Apps: Bridging the Gap Between Web and Mobile
Integrating mindfulness practices helps developers cultivate present-moment awareness, fostering focus, problem-solving, and work-life balance.

Automating Repetitive Tasks: Productivity Hacks for Developers
How to deploy your Next.js apps on Vercel.

Best Practices for Writing Clean and Maintainable Code
How to deploy your Next.js apps on Vercel.