Dynamic OG images and beyond with Cloudflare Workers

Jan 14, 2025

After revamping the site last week, I decided to evaluate the “workers” powering various site features. I’ve been using Cloudflare Workers for both dynamic OG image generation and OEmbed endpoints on this site, with great results. However, it’s 2025, and I wondered if there might be more efficient approaches than my original implementation.

A quick lookup on the internet led me to this great tutorial on Cloudflare’s official site: Generate Dynamic OG Images using Cloudflare Workers. It was time for an upgrade.

The old way and its problems

I had been using a package named workers-og, which is an adaption of @vercel/og for Cloudflare Workers. For logo images and fonts, I used Cloudflare KV to store the data. This combination made it inconvenient to change, update, or test the design.

Imagine the steps required to make a change:

  1. You first have to make sure the HTML template works (satori, which is used in both workers-og and @vercel/og, only supports a limited subset of HTML and CSS features). workers-og uses a different style compared to @vercel/og, making it even more time-consuming.
  2. After that, you will have to manually convert the image into a base64 string, then put it in Cloudflare KV using either the dashboard UI or Wrangler CLI command.
  3. For the fonts, you can only use the Wrangler CLI command to add them to KV.

After all that, you can deploy and test it on the deployed worker. If something fails, you have to repeat the process. What a headache, right?

New features to the rescue

Fortunately, Cloudflare Workers has recently introduced new features that address these challenges.

Static Assets

Currently in beta, this feature allows us to finally have images, fonts, and code in the same project (though assets are stored in a separate directory, they’re managed within the same project). No more jumping around to change things.

”nodejs_compat” flag

The flag enables runtime compatibility features required by the OG image generation library. This allows us to replace workers-og with the official @cloudflare/pages-plugin-vercel-og/api package for OG image generation.

And beyond?

Thanks to Hono, setting up multiple endpoints in a single worker has become remarkably straightforward. This simplifies projects management by allowing us to consolidate everything into a single git repository. The process is even smoother now that Cloudflare has released Workers Builds, enabling seamless CI/CD setup for Workers, similar to what’s available in Pages. This integration makes development, testing, and deployment more efficient than ever.

You can check out my Workers setup at vinhphm/site-workers. Cheers!