🚀 Before we get going
Before we get going on this post about Open Graph SEO in SvelteKit, I should mention this is the second post in a series on SvelteKit SEO. The first post included an introduction to SEO in general and also some details on how to add Twitter metadata to your SvelteKit site pages. This time we will look at Open Graph meta, which is great for optimising how your page looks when it is shared on Signal, Wire, WhatsApp and Slack.
Using Open Graph metadata we can specify which images to display on social media shares as well as customise the text that appears on the sharing card. You have probably seen memes about social media apps doing poor crops of shared images. By using Open Graph meta, we preselect an image which is nicely cropped and avoid a poor reflection on our brand. Here is what we are aiming towards:
This is a screenshot from the Signal messaging app, showing some shares of pages from our test site. I added the Open Graph logo overlay in the image itself, just to make it easier to which images are used where. The Signal App uses our preferred titles and descriptions as well as the nicely cropped square image because we include the right Open Graph meta. Now you have an idea of what we can achieve shall we take a look at how to do it?
🧑🏽🎓 Open Graph Metadata
Let's start by looking at some general Open Graph tags you can use on any page of your site. Following that we look at some more specific to blog posts. Finally we will look at nailing Open Graph SEO SvelteKit social images.
Although Open Graph protocol was developed at Facebook , it has become widely adopted and is used by other apps (as we saw with Signal above). The Twitter tags we looked at in the earlier post look similar to Open Graph ones. In fact the Twitter protocol is loosely based on the Open Graph one. As mentioned in the previous SvelteKit SEO post, you can omit some of the Twitter tags if you include the equivalent Open Graph Tag. As an example you can drop the second tag below and Twitter would knows to use the first one:
General Open Graph Meta
The Open Graph tags you would use on any page of your site are:
og_site_name: This is just the name of your site.
og:locale: This is the language the shared page is written in. If you skip it, Facebook will assume content is written in American English (
en_US), so Americans can leave this one out. If you do need this tag, it should be in
language_TERRITORYformat. I write my posts in British English so use
og:url: The url of the shared page.
articlehere for blog posts and
websitefor other website pages.
og:title: This is an SEO title and will typically be show in the share card. We can set this it to the page title used in the HTML head section (we discussed this in the earlier SvelteKit SEO post).
og:description: You can set this to the page's meta description (see previous post).
fb:app_id: Although you might see this on other pages, it is no longer best practise to include it . It is safe to ignore the warning when testing with Facebook Sharing Debugger (explained in more detail further down).
Article Open Graph Meta
We will take a look at how to integrate that meta into your SvelteKit site in a moment. First, though, here are some additional metadata you can use on article or blog post pages:
article:publisher: if you have a company Facebook page, set this to the URL of your Facebook company page.
article:author: similar to publisher above, but this time you use the personal Facebook page of the author.
article:published_time: use the ISO 8601 date and time format . I set this to the time the article was first published. You generate a date in the right format from the terminal with the
datecommand. To make life easier, I added the alias code below into my
~/.zshrcfile and then just type the command
timestampwhenever I need to invoke it:
This also copies the timestamp to the clipboard. Keen to hear other methods you have for generating timestamps.
article:modified_time: what you would expect it to be (also in ISO 8601 format).
🖼️ Open Graph Images
The world of Open Graph images is not a simple one. You can include multiple images in the meta (this is in addition to the Twitter one we looked at previously), however the order you include them in can affect which ones are used. This is because there is a patchwork of policies on which Open Graph image to use for different apps. Taking this into account, the best strategy is probably to optimise for the apps most used by your audience.
I found what works well for me is to use a Facebook optimised image first and then a square one afterwards. For the first one, I use a 1200 px × 627 px image. Wire and Facebook use this. The second is a square, 400 px × 400 px image. WhatsApp and Signal pick the square image. Telegram, Slack and Discord use the Twitter image. You may get slightly different results if you change the order you include the tags in. Let me know if there's a popular app I have forgotten to mention. We will see how to include the meta next.
🖥 Open Graph SEO in SvelteKit: The Code
So we've gone through what we need to include. Let's look at an example of how we code it up. We will look at a blog post. To make it more concrete we will use the SvelteKit MDsveX blog template.
Step zero is to create the images. You can generate them automatically, though I prefer to crop them myself in a photo editing app to ensure I get a good crop. If you are cropping images automatically, be sure to check the auto images look good. For the test site I used images with no text. For your own site you might want to generate a set of template images (one for each aspect ratio) then programmatically add text to customise them for each page or blog post. Be sure to use the Rainbow tool to make sure you have a decent contrast ratio between any text and the background image if you do this.
Next we crack open some code. To specify which images we want to appear in social shares we use the frontmatter section of each blog post's markdown file:
I am using Imgix to host the images,but you can host them anywhere you like. The images we specify in each post's markdown are used in the
BlogPost template where they are passed to the
SEO component along with other metadata from the frontmatter:
You will see we build up image objects containing the image and it's alt text to pass to the
SEO component. We have included a fallback in case the images and alt were not given in the original markdown file. The next step in the chain is the
SEO component which passes the relevant meta to its
Open Graph SEO in SvelteKit: The Svelte OpenGraph Component
The final piece of the puzzle is the
OpenGraph component itself. This includes all the meta tags we mentioned above:
We include the two images mentioned earlier. In the generated page you might get problems generating WhatsApp social images if, in the page's HTML head section, the Open Graph image tags come after the first
<style> tag. I checked the generated code and this is not an issue here, using SvelteKit. If you are using other frameworks though, and have WhatsApp issues, it is worth checking and re-ordering the elements if needed.
💯 Open Graph SEO in SvelteKit: Test
As always our last step is to test this all works. As with the Twitter meta, there is a tool for checking Open Graph meta. This is the Facebook Sharing Debugger . To use it, you will need to have a Facebook account and to log in.
You can ignore the warning about the missing
fb:app_id property (see above).
🙌🏽 Open Graph SEO in SvelteKit: Wrapup
That's it for this post. We looked at:
- what Open Graph SEO meta you should include in your website pages and posts,
- how to generate Open Graph SEO in SvelteKit,
- testing you Open Graph metadata.
In the previous post on SvelteKit SEO, as well as an introduction to SEO in general, we looked at adding SEO meta for Twitter, which is used for Twitter, Slack and other apps. In the next post in the series we will step up a gear and look at the more advanced Schema.org meta you can start including on your site pages. SchemaOrg is a protocol designed to let browsers understand what a website page is about. Using it can lead to higher ranking in Google and more prominent display of your page in search results pages. Looking forward to running through this!
🙏🏽 Open Graph SEO in SvelteKit: Feedback
Please send feedback! Have you found the post useful? Would you like to see posts on another topic instead? Get in touch with ideas for new posts. Also if you like my writing style, get in touch if I can write some posts for your company site on a consultancy basis. Read on to find ways to get in touch, further below. If you want to support posts similar to this one and can spare a couple of dollars, rupees, euros or pounds, please consider supporting me through Buy me a Coffee.
Finally, feel free to share the post on your social media accounts for all your followers who will find it useful. As well as leaving a comment below, you can get in touch via @askRodney on Twitter and also askRodney on Telegram . Also, see further ways to get in touch with Rodney Lab. I post regularly on SvelteKit as well as other topics. Also subscribe to the newsletter to keep up-to-date with our latest projects.