A while ago, I saw this tweet thread from Simon Willison about how he added social media preview cards to his TILs. He detailed how he did this through a combination of Puppeteer, Vercel, SQLite, and some other stuff I didn’t understand 😅.
Added social media preview cards to my TILs! https://til.simonwillison.net/til/til/pytest_subprocess-server.md
At that time, I was manually taking these screenshots by hand to be included as part of the commit, which is to say it’s not very efficient. Anyway, this stayed on my backlog of “things to explore” for a really long time until recently during this long weekend (where I also took additional PTOs), I had some free time and decided to look into this. The goal is simple, automate creating the preview images for a list of given / defined urls.
I know I just mentioned above that Simon used Puppeteer for his implementation but I just recently picked up Playwright for another project (GlobeTrotte) and figured that it seems like the right tool for this. Also, Playwright has better TypeScript support in general so I decided to opt for Playwright instead. (If you didn’t know, Playwright is built by the same team of engineers who built Puppeteer but has somehow all moved over from Google to Microsoft to build Playwright. There were some uhh interesting discussions about this when Playwright was first announced publicly.)
The minimum code needed for a working version is pretty short. It looks something like this:
Since I might want to use this in multiple places, I figured it’s probably a good idea to create an npm package for this so I can reuse it everywhere (with just some slight config change).
The package itself is really just a CLI tool for now. First, create a json config file based on this doc (depending on what you want from these screenshots). Then, run
npm i -g @globetrotte/altimeter && altimeter config.json and you should see the screenshots show up once the run is complete.
Note: Unfortunately straight up calling
npx @globetrotte/altimeter config.json doesn’t work as of now (unless you already have playwright installed before which can’t be guaranteed) since it won’t trigger the postinstall script to install playwright.
Alternatively, it can also be generated by including the package as a dependency, then import
AltimeterConfig from the package, set the variables, and write the output to a JSON file. Something like this:
Then you can run it with
altimeter config.json and it will start taking the screenshots and saving them into the specified folder.
<meta /> tags
To actually make the images show up when your web page is linked, you need to add the appropriate meta tags depending on which social media you want the image to be fetched and displayed.
Open Graph Protocol
Open Graph Protocol is used on many web services (including Facebook, LinkedIn, Mastodon) to fetch the preview image so you need to add the followings onto your site within the
<head> element. You can also (optionally) specify the image width and height. From my personal experience, it doesn’t matter if they are absolute or relative path as they seem to work either way.
Twitter has its own documentation for the display card. At the time of writing, it seems like they only take absolute path, relative paths don’t seem to work as intended.
You can use this card validator built by Twitter to try if your newly set
<meta /> tags are getting picked up. Unlike just tweeting it out on your profile and see how it work, this would always force fetch from the given link and overwrite whatever cache they already stored of the link.
Honestly, I couldn’t find a good documentation on how to go about setting the image properly. I only found this about setting the
max-image-preview but nowhere does it say how to tell Google which image to use. I looked into how some of the news websites does it instead. NYTimes uses the
image tag as below. The Verge seems to use some sort of Schema.org setup but within a
<script /> tag (instead of
<meta />) and label it as
application/ld+json type. I also checked GitHub (since they actually do one of these screenshot as preview image stuff really well) but I didn’t see any
<meta /> tag that stood out.
I currently use the above package as a CLI on my own blog (specifically this one that you’re reading right now). I didn’t want to have to run a bunch of things manually everytime I write a new post before I deploy so instead, I “delegated” this part of the job to GitHub Action.
- Add the generated directory to .gitignore
- Use Node.js in the GitHub Action (
- uses: actions/setup-node@v1)
- Make sure the devserver site is up and running (in my case,
hugo serve -D)
npx altimeter config.json
- Run build if needed (
- Deploy! 🎉
Note: If you are generating this for static content, you might need to setup a server to serve up those static content on a
localhost address. I recommend following this guide to set up a server (instead of using
This was a fun ride. Honestly, I ran into a few problems here and there (mainly, setting up monorepo in GlobeTrotte and creating CLI with TypeScript) but it’s relatively straightforward otherwise. The actual coding part took less than a day while writing up the documentations (and this blog post) probably took close to double the time.