12 Feb 2019 • 7 min read
I recently asked myself the question "how do link previews in iMessage work, and why doesn't my site have a preview?". I discovered that iMessage link previews, Twitter cards, Facebook link previews, and other social link previews revolve around one standard: the Open Graph protocol.
It says on the Open Graph protocol spec that
the Open Graph protocol enables any web page to become a rich object in a social graph.
A "rich object" is a rich preview of the content of a link. You see these all over the place–here are just a few examples:
Without this protocol, social sites don't have a clear way to understand what content is relevant for a link preview, and link previews are dull and uninteresting. The Open Graph protocol provides a standard way for you as a developer to do a small amount of effort that results in rich, engaging link previews.
You use the Open Graph protocol by adding specific types of meta tags in the head of your page. Much like standard meta information (title
, description
, etc), this allows web crawlers to quickly find out information about your page by only crawling the head.
A basic Open Graph meta tag looks like this:
<meta property="og:title" content="The Trevor Harmon" />
The property
attribute on the tag acts like a key and the content
attribute acts like a value. Every Open Graph meta tag property name uses og:
as the prefix (except for certain Twitter-specific properties–more on that later).
Note: most of the time (outside of Open Graph usage) a meta tag uses name
instead of property
as the identifying attribute. The difference is explained on this StackOverflow post but because the Open Graph protocol uses property
, it seems like that's the preferred usage when working with Open-Graph-related meta information.
There are four basic meta tags that you should include on every page:
og:title
: the title of the page. According to Apple's documentation, this shouldn't include identifying information about the site, but only information about the page itself.og:type
: the type of content of the page. The type you will most likely choose is website
, but there are also other types available if you need something more specific.og:image
: the image that will display on a link preview. This should be an image that is representative of the page's content. It's also helpful to have an image representing the site associated with the link (like a favicon) as a fallback.og:url
: the canonical URL of the page. It needs to be an absolute url.As I implemented these four basic tags, I found out about a couple of gotchas:
og:image
. I would recommend using an absolute url instead.og:url
. Some of the tools I used threw warnings about redirects in the url.In addition to those four basic tags, there are a few more to know about:
og:description
: the Open-Graph-specific description. This lets you include a description catered to social media sharing, instead of using the default description set in typical metadata.og:site_name
: the name of the website ("Facebook", "Reddit", etc).og:locale
: the locale of the website content. This tag defaults to en_US
–change it if your content isn't US centric.There are more optional tags you can use, but these are the most important to know about (for most cases).
Twitter has created a few of its own meta tags that affect Twitter cards (which are generated when you share a link on Twitter). Twitter first looks at typical Open Graph tag (like og:title
), and then looks for any Twitter-specific tags. Here are the Twitter-specific tags you need to know about:
twitter:image
: like og:image
, but for Twitter. Twitter will automatically pull from og:image
if it doesn't find twitter:image
.twitter:description
and twitter:title
: same as twitter:image
.twitter:site
: the Twitter handle of the website. If the link was to a page on the New York Times, this would be the twitter handle of the New York Times (@nytimes).twitter:creator
: the Twitter handle of the content creator. If the link was to a page on the New York Times, this would be the handle of the author of the article.twitter:card
: the type of card displayed on Twitter. If og:type
, og:title
, and og:description
exist on the page, Twitter defaults to using summary
for the card type.If you need more documentation on what Twitter tags exist, you can find a full list in the documentation on their website.
As an example, the open graph implementation for my previous article looks like this:
<head>
<!-- Basic -->
<meta property="og:title" content="What I Learned from a Failed Startup" />
<meta
property="og:description"
content="Four lessons I learned from working as an engineer for a startup that ran out of cash and went bankrupt."
/>
<meta
property="og:url"
content="https://thetrevorharmon.com/blog/what-I-learned-from-a-failed-startup/"
/>
<meta property="og:image" content="https://thetrevorharmon.com/favicon.png" />
<!-- Additional -->
<meta property="og:type" content="website" />
<meta property="og:site_name" content="The Trevor Harmon" />
<!-- Twitter -->
<meta property="twitter:card" content="summary" />
<meta name="twitter:site" content="@thetrevorharmon" />
<meta name="twitter:creator" content="@thetrevorharmon" />
</head>
I've broken out the code according to each section of this article. I'm letting Twitter infer most of the information, and only providing the meta information for the card type and twitter handles. All of that code translates to this nice Twitter card:
Setting up Open Graph metadata isn't challenging, but it makes a big difference for the shareability of your website. Here are some of the tools I used and documentation I read in order to get Open Graph working on my own site.
Get my posts in your feed of choice. Opt-out any time.
If you enjoyed this article, you might enjoy one of these: