If you've been following this website for a while (or, most likely, I've sent you this blog and opengraph image before), you might've seen that I've added opengraph images. For example, this is the opengraph image for my previous post.
An opengraph image from my previous post
Googling
Well, as a good developer you firstly google your issue. You find several answers, including one on the nextjs page itself. Task accomplished, right? No. A big No.
You see, this post assumes you host your site, not statically export it. There was a github issue regarding this, but it was unanswered. After that I've found a reddit post with quite not the thing i needed, but the source of this package from the post got me an idea.
My idea was to generate images using an api route and then screenshot them with playwright. The same idea is used in Maxime Heckel's blog, which would probably work, but i use NixOS, a declarative linux distro. The Playwright package wasn't working correctly. Not only this, but taking screenshots of pages meant that it would slow down the build process drastically. So, i did more googling. And found a github issue. This was exactly what i needed
What the issue suggests
The issue mentions that you can do [...slug]/og.png
route with <ImageResponse>
. Surprisingly, this works. If not a one caveat... You CAN'T use tailwind className in it. Well, I surely won't style the image with usual css, so i found tw-to-css.
Attention
While writing this article i discovered that you can use tw='your-tailwind'
to use tailwind classes. While this works, it's a little buggy and you can't use it for SVGs, so the above is still valid.
With this in mind, I implemented the OG image.
Implementation
So, you create the route as I've written above. My structure looks like posts/[...slug]/og-normal.png
, as I plan to add another OG image for twitter soon. Then, It's just matter of styling. My code looks like this.
Caveats
Some important limitations to consider are:
- You can't use your usual font (or next/font for that matter), you have to host it yourself. I just put it in /public
- You can't use
className
- Debug is not easy
- All classes that do calculations (i.e. space-x, space-y) won't work.
- Lucide icons won't show up, so you should use SVGs
Share it!
As you can see, the Implementation is rather simple. The hardest part is that the information is hard to find online, so it would be nice if you've shared it with your developer friends.
A short off-topic description of what I've added
Changelog
Those posts serve a purpose of being kind-of a devlog for now, so, I thought I'd list changes done
- As you can already imagine, opengraph image.
- I filled main page with actual info
- I've added a posts page with search functionality
- Latest posts now show 3 latest posts, but I might show pinned ones aswell
- I'm about to (hopefully) start indexing this site in google
Search explained
For search I'm using Fuse. It's super simple to setup search. It's just
And, in the input field
Then you just display your items iterating through input
variable. It's that simple.
Note that the search will be client-side.