This guide provides step-by-step instructions for setting up SEO in a Vite React project using React Helmet Async.
- Project Setup
- Installing Dependencies
- Creating the SEO Component
- Implementing the SEO Component
- Usage Examples
- Create a new Vite React project with TypeScript:
npm create vite@latest my-project -- --template react-ts
cd my-project- Install base dependencies:
npm install- Install React Helmet Async for managing document head tags:
npm install react-helmet-async- Create a new file
src/components/SEO.tsx:
import React from 'react';
import { Helmet } from 'react-helmet-async';
interface SEOProps {
title: string;
description: string;
keywords: string[];
image?: string;
url?: string;
type?: string;
author?: string;
}
export function SEO({
title,
description,
keywords,
image = '/meta/default-og-image.jpg',
url = 'https://yourdomain.com',
type = 'website',
author = 'Your Name'
}: SEOProps) {
const siteTitle = 'Your Site Name';
const fullTitle = `${title} | ${siteTitle}`;
return (
<Helmet>
{/* Basic Meta Tags */}
<title>{fullTitle}</title>
<meta name="description" content={description} />
<meta name="keywords" content={keywords.join(', ')} />
<meta name="author" content={author} />
<meta name="robots" content="index, follow" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta charSet="UTF-8" />
{/* Open Graph / Facebook */}
<meta property="og:type" content={type} />
<meta property="og:title" content={fullTitle} />
<meta property="og:description" content={description} />
<meta property="og:image" content={image} />
<meta property="og:url" content={url} />
<meta property="og:site_name" content={siteTitle} />
{/* Twitter */}
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:title" content={fullTitle} />
<meta name="twitter:description" content={description} />
<meta name="twitter:image" content={image} />
{/* Canonical URL */}
<link rel="canonical" href={url} />
</Helmet>
);
}- Wrap your app with
HelmetProviderinsrc/App.tsx:
import { HelmetProvider } from 'react-helmet-async';
function App() {
return (
<HelmetProvider>
{/* Your app components */}
</HelmetProvider>
);
}- Use the SEO component in your pages:
import { SEO } from '../components/SEO';
function HomePage() {
return (
<>
<SEO
title="Home"
description="Welcome to our website"
keywords={['react', 'vite', 'typescript']}
/>
{/* Page content */}
</>
);
}<SEO
title="Home"
description="Welcome to our website"
keywords={['react', 'vite', 'typescript']}
/><SEO
title="Blog Post"
description="An interesting article about React"
keywords={['react', 'javascript', 'web development']}
image="https://yourdomain.com/images/blog-post.jpg"
url="https://yourdomain.com/blog/post-1"
type="article"
author="John Doe"
/><SEO
title="Blog Post Title"
description="Blog post excerpt or summary"
keywords={['blog', 'article', 'relevant', 'keywords']}
type="article"
/><SEO
title="Product Name"
description="Product description"
keywords={['product', 'category', 'features']}
type="product"
image="/products/product-image.jpg"
/>-
Unique Titles: Each page should have a unique, descriptive title.
-
Meta Descriptions: Keep descriptions between 150-160 characters for optimal display in search results.
-
Keywords: Use relevant, focused keywords (though their SEO impact is minimal).
-
Images: Always provide high-quality OpenGraph images for better social media sharing.
-
URLs: Use canonical URLs to prevent duplicate content issues.
-
Use the Facebook Sharing Debugger to test OpenGraph tags.
-
Use the Twitter Card Validator to test Twitter Cards.
-
Use Google's Rich Results Test to test structured data.
- Dynamic Content: Update meta tags based on page content:
<SEO
title={post.title}
description={post.excerpt}
keywords={post.tags}
image={post.featuredImage}
url={`/blog/${post.slug}`}
/>- Default Values: Set up default values for optional props:
const DEFAULT_IMAGE = '/meta/default-og-image.jpg';
const DEFAULT_AUTHOR = 'Your Site Name';- TypeScript Types: Export types for reuse:
export interface SEOProps {
title: string;
description: string;
keywords: string[];
image?: string;
url?: string;
type?: string;
author?: string;
}-
Meta Tags Not Updating: Ensure you're using
HelmetProviderat the root level. -
Images Not Showing: Verify image URLs are absolute paths.
-
Multiple Titles: Check for multiple SEO components on the same page.