Metadata API — Dynamic SEO + OG Cards with generateMetadata
Metadata API — Dynamic SEO + OG Cards with generateMetadata
💡 Why Learn This? — The Part That Makes a Difference for SEO and Social Sharing
metadata export · generateMetadata · File Conventions
1. Static metadata export
Export from the top of each page or layout. Automatically injected into <head>.
2. generateMetadata — For Dynamic Pages
If the same fetch is called in the page body, it is automatically deduped — no extra network cost.
3. title.template in layout.tsx — Automatic Suffix on Every Page
4. File Conventions — Automatic Metadata Linking
opengraph-image.tsx can dynamically generate PNG via ImageResponse. codemaster40 already uses [src/app/opengraph-image.tsx](src/app/opengraph-image.tsx) for dynamic generation.
5. Commonly Used Fields
💡 💡 Top 5 Metadata Tips
1. Set metadataBase in the root layout
Relative paths for OG images and similar assets are automatically converted to absolute URLs.
2. Consistent branding with title.template
Set template: '%s | CodeMaster' at the root — child pages just write 'TypeScript Basics'.
3. fetch calls in generateMetadata are automatically deduped with page fetches
Same URL is called only once. No extra network cost.
4. Use all 4 file conventions — zero lines of code
Just place app/icon.ico, apple-icon.png, opengraph-image.png, and twitter-image.png — metadata links up automatically.
5. OG images should be 1200×630; Twitter card should be summary_large_image
Standard sizes. Validate at: https://www.opengraph.xyz/, https://cards-dev.twitter.com/validator