If you're building a Nuxt app and want your content to stand out on social media, dynamic OG images are a game-changer. Instead of static, generic preview images, you can generate personalized visuals on the fly based on your page data.
Letβs walk through how to create dynamic OG images in Nuxt, step-by-step.
Enjoy!
π€ Why dynamic OG images?
Open Graph images are what platforms like Facebook, Twitter, and LinkedIn use when someone shares your link. Instead of using the same image for every page, you can dynamically generate unique images using page-specific dataβlike titles, authors, or thumbnails.
Example use cases:
- Blog posts with custom titles.
- Product pages showing the actual product.
- User profiles with avatars
π’ Creating dynamic OG Images in Nuxt
There are multiple ways how you can add dynamic OG images to your Nuxt application.
The first option is to use image generation on the fly service such as one of the below:
- Vercel OG Image Generation
- Satori (for generating SVGs)
- Puppeteer (for full-page screenshots)
- Third-party APIs like Cloudinary or BannerBear.
These solutions will help you achieve result pretty fast but there are ways in Nuxt how you can build dynamic OG images efficiently and for that, we can use nuxt-og-image
module from Harlan Winton (as a part of NuxtSeo).
Using Nuxt OG Image module comes with several benefits such as:
- Built-in image generation via Vue components
- No third-party services like Vercel OG or Cloudinary needed
- Works with Tailwind CSS or any styling method
- Auto-generates OG images per route or component
- Zero-config after setup.
- Reusable components for OG layouts.
- Supports dynamic props via query params.
- Fully SSR-compatible.
To use it in Nuxt we need to install it:
pnpm install nuxt-og-image
And add it to the modules
array in nuxt.config.ts
file:
modules: ['nuxt-og-image']
Now, we can start defining our OG Image elements. If we want to use one of the existing templates, we can do so like following:
<script setup lang="ts">
defineOgImageComponent('Nuxt', {
title: 'Nuxt Commerce',
description: 'A high-performance, server-rendered E-commerce app built with Nuxt & Shopify',
theme: '#4ADE80',
headline: '',
colorMode: 'dark',
})
</script>
As a result, if we share a link to our application on social media such as Twitter/X we will see following image:
In my case, one of the existing templates was enough, but you can take custom OG Image generation to the next level by creating your own Vue component template. Read more about it here.
β Best practices
When building dynamic OG images, make sure to follow these best practices to achieve the best results:
- Cache your OG images if possible to reduce server load.
- Validate your OG tags with Facebook Sharing Debugger and Twitter Card Validator.
- Make sure your dynamic endpoint is fast, since social platforms often have short timeouts when fetching OG images.
- Optimize fonts and assets used in your OG components for performance.
- Combine with Nuxtβs runtime config for dynamic base URLs across environments.
π Learn more
If you would like to learn more about Vue, Nuxt, JavaScript or other useful technologies, checkout VueSchool by clicking this link or by clicking the image below:
It covers most important concepts while building modern Vue or Nuxt applications that can help you in your daily work or side projects π
β Summary
With dynamic OG images, your Nuxt site instantly becomes more shareable and professional. Whether it's personalized blog covers, product previews, or user profiles, dynamic visuals give your links an edge on social platforms.
Take care and see you next time!
And happy coding as always π₯οΈ
Top comments (1)
nice!