Open Graph (OG) images are a must if you're sharing content on the Internet. From sites like X/Twitter, to Slack or Discord, a great OG image makes your link share pop.
Examples
I recently built out a couple of OG images for Open Sauced for a couple of features we've rolled out over the past couple of months, Workspaces and Repository pages. They're great features that I encourage you to check out, and I encourage you to share them on socials so our beautiful OG images pop.
For example, here's an OG image for a workspace for jsr. JSR is the new JavaScript registry from the folks from Deno.
Looking at the image for the jsr workspace, there is a template for the image, but there are several dynamic portions to the image.
All the sections denoted by green outlined squares are dynamic.
This dynamic info gets pulled in for the most part from the OpenSauced API.
Other parts are pulled in from the URL, like 30 for the day range, and the description comes from the query string in the OG image URL.
React to generate an image
So, how do we use React to generate an image?
We're using og_edge from my old co-worker Matt Kane (@ascorbic), but og_edge is a direct port of @vercel/og that works on Deno and Netlify Edge Functions which run on Deno.
Satori supports the JSX syntax, which makes it very straightforward to use. Here’s an overview of the basic usage:
// api.jsximportsatorifrom'satori'constsvg=awaitsatori(<divstyle={{color: 'black'}}>hello, world</div>,{width: 600,height: 400,fonts: [{name: 'Roboto',// Use `fs` (Node.js only) or `fetch` to read the font as Buffer/ArrayBuffer and provide `data` here.data: robotoArrayBuffer,weight: 400,
The API for the og_edge module is pretty straightforward. It exposes an ImageResponse constructor with the following options and that's it.
newImageResponse(element:ReactElement,options:{width?:number=1200height?:number=630emoji?:'twemoji'|'blobmoji'|'noto'|'openmoji'|'fluent'|'fluentFlat'='twemoji',fonts?:{name:string,data:ArrayBuffer,weight:number,style:'normal'|'italic'}[]debug?:boolean=false// Options that will be passed to the HTTP responsestatus?:number=200statusText?:stringheaders?:Record<string,string>},)
To build out these OG images, we have a background image, some icons, like a star and fork icon, and we also pull in the repository organization or user's avatar. With a bit of vanilla CSS, we can position things just right. We also pull in the Inter font as that's what we use at OpenSauced.
As far as I know, og_edge does not support Tailwind like @vercel/og does. Not a dealbreaker at all, but just something to be mindful of.
One other thing we do is set cache headers as these are dynamic images where the data changes over time. Having said that, some social networks cache OG images very aggressively.
headers:{// cache for 2 hours"cache-control":"public, s-maxage=7200","content-type":"image/png",},
Show me the code
Here's the pull requests for the initial work on these two OG images.
Now if you share a workspace dashboard link, it will generate a preview image when shared on socials, Slack etc. Repository statistics have easier to read numbers and also save on real estate, e.g. 1100 becomes 1.1k. If the title is too long it's truncated and has ... at the end. Same for the description.
What type of PR is this? (check all applicable)
[x] 🍕 Feature
[ ] 🐛 Bug Fix
[ ] 📝 Documentation Update
[ ] 🎨 Style
[ ] 🧑💻 Code Refactor
[ ] 🔥 Performance Improvements
[ ] ✅ Test
[ ] 🤖 Build
[ ] 🔁 CI
[ ] 📦 Chore (Release)
[ ] ⏩ Revert
Related Tickets & Documents
Closes #2932
Mobile & Desktop Screenshots/Recordings
Steps to QA
go to any public workspace's dashboard in the deploy preview.
Copy the URL.
Paste it on socials, Discord, or Slack.
The OG image unfurls showing the social card.
Added to documentation?
[ ] 📜 README.md
[ ] 📓 docs.opensauced.pizza
[ ] 🍕 dev.to/opensauced
[ ] 📕 storybook
[x] 🙅 no documentation needed
[optional] Are there any post-deployment tasks we need to perform?
[optional] What gif best describes this PR or how it makes you feel?
Beautiful and dynamic OG images are a must if you're looking to stand out when sharing links on socials, and og_edge and @vercel/og are great options if you also want to leverage your existing React skill set.