RAFIQUL.Full-Stack & E-Commerce
Back to Articles
Web DevelopmentSeptember 8, 20266 min read

Building Dynamic OG Images for a Next.js Blog

I replaced one static social preview with a cached Edge route that renders each Next.js post title into its own OG image automatically.

Rafiqul Islam Rabbi
Rafiqul Islam Rabbi
Full-Stack Web & E-Commerce Architect
Building Dynamic OG Images for a Next.js Blog

For a while, every post on my Next.js blog shared the same social preview image. It was a decent branded card with my name, a dark background, and enough contrast to look acceptable in Slack or LinkedIn. Acceptable was also the problem. A reader could share three different articles and see the exact same image each time. The post title was doing all the work, and sometimes it was not even visible in the preview.

I wanted each article to have its own image without adding another publishing chore. I did not want to open Figma, duplicate a template, export a PNG, upload it somewhere, and then remember to update the metadata. The title, category, and slug already existed in the blog data. The useful solution was to make the social image another view of that same data.

The static image had quietly become a publishing bottleneck

The first version of the blog used a single image in the root metadata configuration. That was simple and reliable, which is why I left it alone longer than I should have. The technical setup was not broken. The workflow was.

When I published a new post, I had to think about the page title, description, canonical URL, and image metadata. If I wanted a custom preview, there was a second mini-project hiding after the article was finished. I tried making a reusable image template in a design tool, but that created a different kind of maintenance. A title that looked fine in the browser could wrap badly on a 1200 by 630 card, and a late title edit meant another export.

The decision was to generate the image at request time from the post slug. I created a dynamic route at /api/og/[slug]/route.ts and used ImageResponse from the Vercel OG tooling. The route would receive the slug, look up the post, and render a consistent card using the title and category. This kept the design fixed while allowing the content to change.

Connecting the route to the same post data

I was careful not to create a second metadata system just for images. The route reads from the same source that builds the article page. In my case, that means the slug resolves to the post record, then the route pulls the title and category before constructing the image response. If a title changes in the content source, the image has a chance to reflect that change without a manual asset update.

That sounds obvious, but it prevents a common kind of content drift. A blog page can show one title while its social image still contains an older version. I have seen that happen when an image is treated as a detached design file. It is not usually a dramatic bug. It is just one more small mismatch that makes a site feel less maintained.

The visual layout stayed intentionally boring. I used a fixed canvas, a brand background, a small category label, the post title as the main element, and a compact site identifier. The title needed the most room, so I tested longer headings instead of tuning the design around a short example. Some headings looked good at one line and became awkward blocks at two or three lines. I adjusted the font size and container width until the card still had breathing room when the title was less cooperative.

At first I tried to make the route too clever by adding more decorative elements and conditional layout rules. That was a wrong turn. Every extra visual condition made wrapping harder to predict, especially for titles with punctuation or long technical names. I removed most of it. The final card is less exciting in isolation, but it is easier to recognize and much harder to break.

The Edge runtime caught the detail I had missed

The route itself was straightforward until I added a custom font. In a normal server environment, I would be tempted to point the renderer at a local font path and move on. The Edge runtime does not work that way. The font needs to be available as binary data, so the font file has to be included in the project and fetched explicitly before it is passed into ImageResponse.

My first attempt used a familiar local path and produced an error that was not especially helpful. The image route worked when I removed the custom font, which made the problem look like a layout issue for a few minutes. It was not. The edge function simply could not read the font in the way a regular Node process might.

I moved the font file into the project, loaded it with a fetch-based approach, converted the response into an ArrayBuffer, and supplied that data to the image renderer. After that, the font appeared correctly. I also checked the deployment rather than trusting the local result. An asset that exists in a local filesystem is not automatically available in the deployed Edge environment.

There was another concern around the post lookup. An OG request should not fail noisily just because a slug is missing or a draft was shared accidentally. I added a fallback response for unknown posts instead of allowing the renderer to receive undefined title data. It is not a replacement for fixing the URL, but it gives the request a controlled result and made debugging much less annoying.

Caching made the small route behave like a finished feature

Once the image rendered correctly, I noticed that the route could generate the same card repeatedly. A post slug and its metadata usually do not change between every social crawler request. Rendering the image again for each request would waste time and resources, especially when platforms fetch the same URL more than once while inspecting a link.

I added caching to the generated response and treated the image as a derived asset. This introduced a tradeoff: if I edit a title, an old cached image may remain visible until the cache is refreshed or expires. I accepted that because the alternative was making every request expensive. For title changes that need to appear immediately, I can invalidate the relevant route or use a cache policy that fits the publishing workflow.

The final step was connecting the generated URL to the page metadata. Each article now points its Open Graph image field at its own slug route rather than the single global image. I tested direct requests, missing slugs, long titles, special characters, and the previews generated by different social platforms. The platforms do not all display images identically, so I checked the actual cards instead of assuming a correct 1200 by 630 response guaranteed a perfect preview.

The main lesson was that the gradient and typography were the easy part. The valuable decision was keeping the image tied to the post data and treating the Edge route as part of the publishing system. Dynamic OG images removed a manual design step, exposed a font-loading edge case, and forced me to think about caching and failure states. Now a new article gets a new preview as part of the same build path, which is exactly where that responsibility belongs.

Rafiqul Islam Rabbi
Written By

Rafiqul Islam Rabbi

Full-Stack & E-Commerce Architect building high-converting Shopify stores, WordPress platforms, and Next.js web applications.

Work With Me

LET'S
WORK
TOGETHER!

Have a project in mind or just want to say hi? I'd love to hear from you and build something remarkable.