-
Notifications
You must be signed in to change notification settings - Fork 395
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom OG images #2695
Custom OG images #2695
Conversation
Amazing work! Is this mergeable or do we have anything else to do here? (We can split off the other unfinished open graph images into another PR since some still need to be designed - mostly the static ones.) |
@corywatilo Currently mergeable. We can get the rest added later! |
Going to merge, but I did a quick run-through and found a few minor things which I'll add to a future issue for when we implement the rest of the custom images! Handbook/Docs
Blog
Next stepsThese seem to be broken |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀
Outsider chiming in with 2 things:
|
That's lovely feedback @yeldarby - if you'd like to read about how we got here, there's a post coming out soon (sneak preview) - we're thinking about productizing this feature too as it's own little open source project, so stay tuned |
Changes
Dynamically creates custom Open Graph images based on page content for docs, handbook, customer, and blog post pages.
Before
After
This is cool, but how does it work?
Once the Gatsby build process is complete, we pass an HTML string containing some page data to Puppeteer for each page; it screenshots each page and saves it. That new image URL is used instead of the standard
banner.png
.A semi-detailed overview of what's happening
Gatsby has a really cool API that allows you to hook into the last point of the build process:
onPostBuild
. This is where all of the magic takes place.We first query for each page that we want to add a custom Open Graph image to. Right now, that's docs, handbook, customer, and blog posts.
From there, we do some initial setup:
After setup is complete, we finally iterate through each queried page, set the content of the Chromium page with an HTML string, screenshot the page, and save it. Here's what that looks like for blog posts:
What's happening in this for...of loop, though?
Since the site isn't technically "live" yet, we don't have access to a public URL to insert into our HTML template. The workaround is embedding it in an
<img />
element as Base64. You'll see this workaround in a few other places in the code.We have an asynchronous function called
createOG
. This handles setting the Chromium page content, screenshotting, and saving the image. It takes an HTML string to set the Chromium page content and a slug to create the file name of the screenshot.Each page type (handbook, docs, blog, etc.) has a custom HTML template based on these designs (S/O @corywatilo). These templates are located in
/src/templates/OG
. They each take different data based on the design and return an HTML string. Take a look atblog.js
to see what's happening in the above example.That's it!