TL;DR: Generating share images can be done in 3 steps:
- create a preview page;
- screenshot it (with Puppeteer);
- add the image in the page’s metatags.
When you spend hours writing a blog post, you want it to stand out, not to be lost in a feed. That’s why having a social share image attached to your blog post is crucial. People are drawn to visual content. Our brains respond very quick to images in comparison to plain text.
The idea behind this post is to show you how to generate preview images like this for your Gatsby.js website.
Social share images are used by social media (Twitter/Facebook/LinkedIn…) and also some conversational applications (Slack/Telegram/WhatsApp/…).
Before we start, keep in mind that Gatsby is static website generator. We can’t generate images “on the fly” as we do with other dynamic languages (i.e. PHP…).
Step 1: create the preview page
The preview page is meant to be screenshotted and only screenshotted!
Your blog post page should be generated with something like createPage()
. So, we are going to
duplicate this statement to create the preview page.
💡How to disable this page in production?
I guess you don’t want to see this page is production right? If you wrap the createPage
in the
following “if condition”. So, the page will be only accessible with gatsby develop
command.
Then, we need to create the blog-post-share-image.js
. In the GraphQL query, fetch fields you want
to use (post title, time to read…). And render everything in a rectangle (i.e. 440 x 220 pixels).
If you want, here’s the template I use for maxpou.fr. Now, use your HTML/CSS skill to make the nicest preview 💪.
After that, the preview should be accessible at
http://localhost:8000/your-article-slug/image_share
.
Step 2: screenshot the preview
To take a screenshot, I use Puppeteer. Here’s a part of the script I am using to take a screenshot:
…and in the package.json, make the script available as a command.
We are now ready to create the picture! To generate the preview image, open 2 terminals:
- Terminal 1:
npm run gatsby develop
; - Terminal 2:
npm run generatePostPreviewImages
.
You should have generated some preview images (1 per blog post). The script above put the image directly in the blog post folder.
Step 3: integrate the picture in the DOM
The social image is now generated. Before you add it in the HTML’s body, don’t forget to link the image with the blog post.
When you render your blog post, you can add the image generated in an SEO component.
The SEO component purpose is to manage all the meta-information located in the page’s <head>
. If
you don’t have this component, you can get some inspiration from the
Gatsby-starter-blog.
Also, feel free to copy/paste
the code I wrote for my website.
At the end, you should have something similar in your HTML’s body:
and… tadaa it should works 🎉
Conclusion
If you want to check your metatags, you can use Twitter cards Validator. To test the Open Graph tags, I usually send a message to myself on Slack.
That’s it for today! I hope this post will help you to generate your social images share. I copied all examples from my Gatsby starter/theme: gatsby-starter-morning-dew.
About the author
Hey, I'm Maxence Poutord, a passionate software engineer. In my day-to-day job, I'm working as a senior front-end engineer at Orderfox. When I'm not working, you can find me travelling the world or cooking.
Follow @_maxpou