📖 Scenario: You are building a simple Astro website for a blog. You want to make sure that when people share your blog posts on social media, the right title, description, and image show up. This is done using Open Graph and social metadata in the HTML head.
🎯 Goal: Create an Astro component that sets up Open Graph and social metadata tags in the HTML head for a blog post page.
📋 What You'll Learn
Create a metadata object with title, description, and image URL
Add a variable for the site URL
Use Astro's <head> slot to add Open Graph meta tags
Include Twitter card meta tags for better social sharing
💡 Why This Matters
🌍 Real World
Social media platforms use Open Graph and Twitter card metadata to show rich previews when users share links. Adding these tags improves how your site looks on social media.
💼 Career
Web developers often add social metadata to websites to improve user engagement and SEO. Knowing how to add these tags in Astro is useful for frontend and full-stack roles.
Progress0 / 4 steps
1
Create the metadata object
Create a constant object called metadata with these exact properties and values: title set to "My Astro Blog Post", description set to "This is a blog post about Astro framework.", and image set to "/images/astro-post.png".
Astro
Hint
Use const metadata = { title: "My Astro Blog Post", description: "This is a blog post about Astro framework.", image: "/images/astro-post.png" }
2
Add the site URL variable
Add a constant string variable called siteUrl and set it to "https://myastroblog.com".
Astro
Hint
Use const siteUrl = "https://myastroblog.com";
3
Add Open Graph meta tags in the head
Inside the Astro component, add a <head> section that includes these meta tags exactly: og:title with metadata.title, og:description with metadata.description, og:image with the full URL combining siteUrl and metadata.image, and og:type set to "article".
Astro
Hint
Use meta tags with property="og:title", og:description, og:image, and og:type inside the <head> section.
4
Add Twitter card meta tags
Inside the same <head> section, add Twitter card meta tags: twitter:card set to "summary_large_image", twitter:title with metadata.title, twitter:description with metadata.description, and twitter:image with the full URL combining siteUrl and metadata.image.
Astro
Hint
Add Twitter meta tags with name="twitter:card", twitter:title, twitter:description, and twitter:image inside the <head> section.
Practice
(1/5)
1. What is the main purpose of adding og: tags in an Astro page's <head> section?
easy
A. To control how the page appears when shared on social media platforms
B. To add custom fonts to the page
C. To improve the page's loading speed
D. To enable server-side rendering
Solution
Step 1: Understand the role of og: tags
og: tags are Open Graph tags used to define how content is displayed on social media.
Step 2: Identify their placement and effect
These tags go inside the <head> and control preview title, image, and description on social platforms.
Final Answer:
To control how the page appears when shared on social media platforms -> Option A
Quick Check:
Open Graph tags = social media preview control [OK]
Hint: Remember: og: tags shape social media previews [OK]
Common Mistakes:
Confusing og: tags with CSS or fonts
Thinking they improve page speed
Placing tags outside the <head> section
2. Which of the following is the correct syntax to add a Twitter card title in an Astro page's <head> section?
easy
A.
B.
C.
D.
Solution
Step 1: Recall Twitter card meta tag syntax
Twitter card tags use name="twitter:..." attribute, not property.
Step 2: Check attribute order and correctness
The correct syntax is <meta name="twitter:title" content="..." />. Attribute order does not affect validity but standard is name then content.
Final Answer:
<meta name="twitter:title" content="My Page Title" /> -> Option D
Quick Check:
Twitter card title uses name="twitter:title" [OK]
Hint: Twitter meta tags use name=, Open Graph uses property= [OK]
But the image does not show on social shares. What is the likely problem?
medium
A. The tag is missing a closing slash />
B. The property attribute should be name
C. The content URL is relative; it should be an absolute URL
D. Open Graph tags cannot specify images
Solution
Step 1: Check the image URL format
Open Graph requires absolute URLs for images to display correctly on social platforms.
Step 2: Verify tag correctness
The tag uses correct property attribute and closing is optional in HTML5; the main issue is the relative URL.
Final Answer:
The content URL is relative; it should be an absolute URL -> Option C
Quick Check:
OG image URL must be absolute [OK]
Hint: Use full URLs for og:image to ensure visibility [OK]
Common Mistakes:
Using relative paths for og:image content
Confusing property and name attributes
Worrying about self-closing slash in HTML5
5. You want to set up social metadata in Astro so that: - Facebook uses Open Graph tags - Twitter uses Twitter card tags - Both show the same title and description
Which is the best way to write these tags inside <head>?
hard
A.
B.
C.
D.
Solution
Step 1: Use correct attributes for Open Graph and Twitter tags
Open Graph tags use property="og:...", Twitter tags use name="twitter:...".
Step 2: Ensure matching titles and descriptions for both
Set the same content values for both sets of tags to keep consistency.
Final Answer:
Use property for og tags and name for twitter tags with matching content -> Option B
Quick Check:
OG = property, Twitter = name, same content [OK]
Hint: OG uses property=, Twitter uses name= for meta tags [OK]
Common Mistakes:
Mixing property and name attributes between OG and Twitter
Using different content values causing inconsistent previews
Omitting either OG or Twitter tags
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.