The aesthetic of a website on social networking can be enhanced by using Open Graph Images. it is possible to manage how a website is portrayed when it’s shared on social networking platforms by defining these images using a collection of meta tags on the head part of the page.

It is easy to notice the advantages of using Open Graph images when comparing a website with defined Open Graph images with one without. When shared on social media, the website with an Open Graph image will display an aesthetically pleasing and representational image, but the website without an Open Graph image will either display no image or a random image from the website's content. The user's opinion of the website and the likelihood to click on the shared link may be considerably impacted by this.
Setting up open graph image/social card generation on the next app
To create social cards there’s a plugin provided by vercel.
For Next.js implementations, make sure you are using Next.js v12.2.3 or newer
Create an API endpoint by adding og.tsx under /pages/api. Then paste the following code:
Then, based on the Open Graph Protocol (https://ogp.me/#metadata), create the web content for your social media post as follows:
- Create a <meta> tag inside the <head> of the webpage
- Add the property attribute with value og:image to the <meta> tag
- Add the content attribute with value as the absolute path of the /api/og endpoint to the <meta> tag
Adding a dynamic touch to it.
To make the Open Graph image generation dynamic, developers can pass data into the API endpoint as query parameters and generate images based on that data. This can be done by creating a
<meta> tag in the <head> of the webpage and setting the property attribute to og:image and the content attribute to the absolute path of the /api/og endpoint, along with any desired query parameters.
and on the og.tsx page
this way you can customize the og image per page.
Conclusion
In this article, we have examined how to use Vercel and Next.js to generate and output Open Graph images for social media sharing. We have seen how to set up a Next.js app with Vercel and use the @vercel/og plugin to generate Open Graph images. We have also explored advanced techniques for customising the Open Graph image functionality of a Next.js app with Vercel, such as generating dynamic images.
I hope this article has been helpful! If you have any questions or would like to learn more about using Vercel and Next.js for Open Graph images, be sure to check out the Next.js documentation and the Vercel documentation on og images
Thinking about generating an image for Sanity's blog?
Here's a quick tip, it says the image is 1200x700px - it's actually closer to 1200x758px if you want it to look identical to the Roboto Studio open graph.