New post 📮 I was tired of building 474 separate pages for Open Graph Images
Zach Leatherman 2929 replies 3232 likes99 reposts11 quote Original on MastodonOriginal on BlueskyAlso posted to Mastodon.
29 replies
@zachleat This is really smart. I just nuked mine recently because I was fed up of making the ~1000 pages it needed. I'll keep this in mind if i want to do them again.
@robb thanks! I forgot to mention in the post that it also solved some sneaky problems with seeing 404 errors if my local build requested a production URL screenshot before it was pushed! If that happens I just show a default now (better)
@zachleat Jokes on you, I can make whatever OG image I want with any text I want :P
@khalidabuhakmeh if it ain’t hosted on my domain it’s a deepfake
(and you could do that before!)
-
@khalidabuhakmeh that’s because I am your dad
@zachleat Did you ever get those cigarettes you went out for? Must have been difficult to find.
@khalidabuhakmeh *backs away slowly*
-
i'm confused, are you generating the same 474 images every build (or worse, every request!)?
- now it’s 1 page for 474 unique images (cached in the screenshot service, not generated every time or every build)
- previously it was 474 pages for 474 unique images (also cached in the screenshot service, not generated every time or every build)(I removed 473 pages from my build)
ah gotcha, nice
Cool! If the background image is always the same, you can also speed up image generations a loooot by not spinning up a whole browser, but justing using canvaskit-wasm in Node/Deno/Bun, e.g. via github.com/mastrojs/og-...
yeah! I wanted a 1:1 with a regular website as much as possible in my workflow so I didn’t have to worry about browser support as much in the library
yeah, as the website changes you'd have to periodically update the background png somehow. Although that could be automated as well ;)
@zachleat Ooh, clever! I should see if this helps get my build times down a little… thank you for sharing!
@chriskirknielsen you’re very welcome!
@zachleat So, to be clear: you are still building 474 open graph *images*, you're just not building 474 separate static HTML pages just to screenshot them to make the images.
You're using the Chromium-based screenshot service as the build tool instead of using eleventy plus also the screenshot service.
That seems like a sensible use of resources. But its not quite as impressive as you implied yesterday; your total build time including the screenshots is still proportional to page count.
@AmeliaBR I suppose! I don’t include the screenshots in my mental model of build times because they aren’t “blocking” build times, they happen independently as needed (but your point is valid!)
@zachleat If the screenshot images are only generated on demand by your image caching layer, that also I guess means you're not necessarily re-building images for your entire archive of posts every time you tweak something.
But be careful about following that logic too far: you could end up re-inventing PHP just to speed up the build times on your "static" website!
first of all: https://www.zachleat.com/twitter/1257414895110369280/
second of all: https://www.zachleat.com/twitter/1304556448035278848/😅
Ohhh that's clever! Took me a trip to your repo to understand, but it's all clicked for me now 🥳 Adding the json to the page is the same way I do a "go to random page" feature - great idea! And nice way to keep it secure so folks can't just generate any OG image with URL param shenanigans.
Thanks for the write up Zach!
you’re very welcome (and thank you for the kind words!)
The screenshot service is custom 11ty service older post references on netlify or something else?
Was just thinking of having a utility like this in my design system docs.
Ie render screenshot of a component dynamically and give it a URL. Useful for marketing/comms team etc
github.com/11ty/api-scr... is the one! currently on vercel though