The internet is full of links that lead to other places vying for your attention.
Wouldn't it be nice if we knew where those links took us before we clicked them?
Rich link previews give us instant context without leaving our current flow. They transform basic URLs into interactive elements that boost engagement by surfacing key metadata upfront.
- Is that article worth reading?
- Is that YouTube video clickbait?
- Is that Tweet a banger?
You don't have to guess when a link gets presented with a small preview.
And while some platforms (like Instagram) might break their OG tag support, we can build better, more reliable experiences in our own apps. Or even in Telegram chats.
Let me show you how to implement robust link previews using the free metadata.vision API and React Server Components (RSC).
metadata.visionGet OG metadata for any website with a simple API.metadata.visionYou have a prime example right here. Which link would you rather click?
Demo of what we’re building
Getting the data
Let’s start by creating a server function that will fetch the OG metadata for a link.
Rendering the link preview
Now we can create a React Server Component that will render the link preview.
- By default, we will show a big video preview if the link has a video.
- If the link has no video, we will show a big image preview.
- If the link has no video or image, we will show a simple link with a favicon next to the title.
Customizing
Let’s give it some props that will let us style it differently depending on what kind of metadata we want to show.
noVideo
falls back to the image preview.noImage
falls back to the simple link with a favicon.compact
will make the image or video smaller and show it alongside the title and description.
On smaller screens, the compact
prop will not affect the layout as to not
cramp the media and text too much.
Final code
Here is the code for our component, styled using Tailwind CSS:
This should give you a good base to work with. Tweak the styles to your liking or remove Tailwind altogether.
If you found this useful, follow me on Twitter or subscribe to the newsletter.
Thanks to Jakub Ziemba and Brandon Johnson for feedback on an earlier draft of this post.