Open Chart images. Those little images that appear when your website is shared on social networks. The ones for my website were … bad. My default was to utilize one, big, dull top quality image for whatever– which offered no additional worth and simply jumbled up y’ alls feeds (sorry!). I wished to make them much better and here’s what I did.
The Lazy Technique #
While constructing a screenshot cloud service for 11ty. dev, it ended up being apparent that I need to include an Open Chart size to utilize screenshots as Open Chart images. You can see this in usage on the Eleventy docs pages:
- Browse to
- With the screenshot service in location, it’s simply one line for a customized Open Chart image:
<< meta residential or commercial property=" og: image" material=" https://v1.screenshot.11ty.dev/https%3A%2F%2Fwww.11ty.dev%2Fdocs%2F/opengraph/">>
- The social card image appears like this:
For the quantity of work this required to setup, it was absolutely worth it (one
<< meta>>!). However I wished to take it to the next level for my individual blog site (with a long term objective of perhaps persuading my coworkers to utilize something like this on netlify.com).
The Underwhelming Technique #
In the past, I did experiment a bit with utilizing the SVG to raster image function of Eleventy Image (through
sharp) however it wasn’t rather what I desired. It had the advantage of being much faster than HTML-based techniques (typically based upon Puppeteer) however I wound up needing to slog through a lot of hand-written SVG to get it to look how I wished to look. I’ll confess, I’m not as efficient at hand-authoring SVG as I ‘d like.
The Fancy Technique #
With those experiences under my belt, I chose to customized style a page on my website (source code) that would repeat over each post and might act as the target for my screenshot service. This offered a number of advantages:
- I can utilize all of the existing HTML/CSS/JS/ Font styles currently in usage on my website. That implies phony internet browser chrome, customized Twitter avatars, Lighthouse ratings, appeal ranks, to name a few!
- I do not need to run Puppeteer in your area. I attempt to prevent this when possible– it is incredible however it decreases the develop (a lot).
Each specific post has a devoted Open Chart image page, for instance
/ opengraph/web/automatic-opengraph/ ( Fair caution: this page is not utilizing responsive website design concepts. It is particularly developed for the 1200 × 630 element ratio).
If I desire a customized background image for a single post’s open chart image, I include front matter with the image url:
openGraphBackgroundImage: / og/sources/graphpaper. jpg
Next, in my base Eleventy design file (source code) I point the Open Chart page for each particular post towards the screenshot service to record the image:
It appears like this:
I desired the image to have the post title, my avatar, the released date, and the URL of the page.
For reward metadata, here’s one with a Lighthouse rating connected (upgraded frequently through Speedlify), a webmentions count, and even the page weight (e.g. 226 KiB, likewise driven by Speedlify):
Bonus Offer Functions #
DevTools Gadget Emulation #
Within Chrome Devtools, I included a customized Replicated Gadget particularly for Open Chart image formats to evaluate the designs.
Social Card Sneak Peek #
I included a little
<< information>> on the bottom of each post so that I might evaluate the social card image sneak peek. I’ll most likely pivot this to a link-based sharing widget eventually or integrate into my existing Retweet to Share element.
On-demand Home Builder Cache Busting #
The Eleventy Screenshot service is an independent task that utilizes On-demand Contractors to cache screenshot image material to Netlify’s CDN. That cache is quite sticky. The most convenient method to revoke the cache is to activate a Screenshot service develop on Netlify, however that would reset the cache for all screenshots conserved by the service, which is too heavy. It has actually been a typical ask for a more granular Netlify API to reset pieces of the cache however throughout the advancement of this task I understood that we can manage this by hand on the app side utilizing our old good friend, the Cache Buster URL.
If the old URL appeared like this
https://v1.screenshot.11ty.dev/https%3A%2F%2Fwww.zachleat.com%2Fopengraph%2Fweb%2Fcomprehensive-webfonts%2F/opengraph/, I can include an approximate cache busting secret to the end of it (prefixed by a highlight to determine it throughout the URL).
If I produced that essential with today’s date (e.g.
https://v1.screenshot.11ty.dev/https%3A%2F%2Fwww.zachleat.com%2Fopengraph%2Fweb%2Fcomprehensive-webfonts%2F/opengraph/_20210803/), it would create brand-new images every day without the screenshot service requiring to revoke the whole cache. (Some related discussion about this on Twitter)