Dynamic Social Meta Tags
You'll learn how to generate unique Open Graph and Twitter Card meta tags for every page so each URL shares perfectly on social media.
When you build a single-page application (SPA) or server-rendered app with multiple routes, each page needs its own social sharing identity. A blog post about coffee shouldn't share the same image and description as one about bicycles—yet both live in the same app.
The Core Challenge
Static meta tags in your HTML <head> work fine for single-page sites, but multi-page apps need dynamic meta tags that change based on the current route or content.
Think of it like a chameleon: the same app shell adapts its social "appearance" depending on what page the user visits.
Implementation Approaches
In Server-Rendered Apps
The server generates unique meta tags before sending HTML to the browser. When someone shares your URL, social media crawlers see complete, page-specific tags immediately:
// Server-side (conceptual)
<meta property="og:title" content={article.title} />
<meta property="og:description" content={article.excerpt} />
<meta property="og:image" content={article.coverImage} />
<meta name="twitter:card" content="summary_large_image" />
In Single-Page Apps
Use JavaScript to update meta tags when routes change. Libraries help you manipulate the document head dynamically as users navigate:
// On route change
document.querySelector('meta[property="og:title"]').content = newTitle;
document.querySelector('meta[property="og:image"]').content = newImage;
Important limitation: Social media crawlers often don't execute JavaScript, so pure client-side updates may not work for sharing. This is why server-rendering or pre-rendering is preferred for pages you want shared.
Data Sources
Pull dynamic values from:
- Route parameters (
/posts/123) - CMS or API responses
- Database queries
- Static data files
Key Takeaway: Dynamic social meta tags let every page in your app share uniquely on social platforms—server-rendering ensures crawlers see them, while SPAs need pre-rendering or SSR for reliable social sharing.