How to create dynamic og image in NextJS
May 7, 2024
Open Graph (OG) images are the images that displayed when your web page is shared on social media platforms like Facebook, Twitter, and LinkedIn. A good OG image can grab attention and drive traffic to your website.
Next.js provides the ImageResponse feature that allows you to easily create dynamic OG images. Dynamic OG images can change based on your webpage content, such as title, description, and featured image.
Lets make it !
Create an assets (optional)
If you want to use a different font you need to download the font first and store it here assets/fonts/{your_font}.ttf
Create og route
You need to create route app/api/og/route.tsx
inside this route you can import the ImageResponse from next/og
The Edge runtime is used for Middleware (routing rules like redirects, rewrites, and setting headers).
if you have custom fonts you need to fetch it like this, you can skip this step if you dont have the custom fonts
create a GET
request function
again if you have the custom fonts you need to await the font first, after that you need to get the title
or type
(type
is optional) from the url, if the title is not found then it will return "No title provided, {status: 500}"
.
If the title length exceeds 60 characters, the code truncates it to the first 60 characters and adds an ellipsis (...). This ensures the text fits comfortably within the image.
Finally the route is returning an ImageResponse()
, inside ImageResponse there is two parameters (ReactElement, Option)
.
The ReactElement
is for styling your dynamic og image and the Option
is for configure your image like the width, height and of course the fonts. ReactElement in ImageResponse is a bit different they dont use className
instead they use tw
For example i created this ImageResponse()
:
you can test or create an og image here [https://og-playground.vercel.app/] if you want, remember if you using tailwind its not 100% supported, there are few classes that is not supported yet
Export dynamic metadata in your dynamic page
Next you need to export the generateMetadata
if you are using typescript the type of params should be your dynamic data props and it return Promise.
next you need to fetch your data inside the generateMetadata
function, the getWritingFromParams
is my function to get the writing data, and dont forget to error handling it if it doesnt return the data.
after that you need to create a new query to your searchParams
URLSearchParams is a built-in JavaScript class that provides methods and properties for working with query string parameters of a URL.
ogSearchParams.set("title", post.title)
This line is simply adds a new query parameter to the ogSearchParams object. The set() method of URLSearchParams is used to set a parameter's value. In this case, the parameter key is "title", and its value is obtained from the post.title variable. This is typically used to include dynamic data in the query string.
and then just return the metadata
Insert the metadataBase
in your app/layout.tsx
insert metadataBase and the value should be your website link
like this :
Result :