More HTML answers

by Danny
Published 3 min read

Open Graph meta tags tell chat apps and social sites which title, description and image to show when someone shares your link. Here are the tags you need and how to test them.

The short answer

#

Add a few meta tags with a property attribute to the head of each page. Use an absolute URL for the image, ideally 1200 by 630 pixels. Add twitter:card so X shows a large image.


_10
<head>
_10
<meta property="og:type" content="article">
_10
<meta property="og:title" content="How we cut our build time in half">
_10
<meta property="og:description" content="Three changes that took our CI from 12 minutes to 6.">
_10
<meta property="og:url" content="https://example.com/blog/faster-builds">
_10
<meta property="og:image" content="https://example.com/og/faster-builds.png">
_10
<meta property="og:image:alt" content="A chart showing build time dropping from 12 to 6 minutes">
_10
<meta property="og:site_name" content="Example Engineering">
_10
<meta name="twitter:card" content="summary_large_image">
_10
</head>

Why it happens

#

When you paste a link into Slack, WhatsApp, LinkedIn, Discord or similar, a bot from that service fetches the URL and reads the HTML head. It looks for Open Graph tags, a format originally created by Facebook that most services now support. If it finds none, it falls back to the title element and perhaps the first image it can find, or shows a bare link.

These bots usually do not run JavaScript. If your tags are added by client-side code after the page loads, the bot never sees them. The tags must be in the HTML that the server sends.

How to fix it

#

Add the core tags to every page

Four properties form the minimum the Open Graph protocol asks for: og:title, og:type, og:image and og:url. Add og:description as well, because almost every service shows it.

  • og:title: the headline for the card. It can be shorter than your page title, without the site name.
  • og:type: use website for most pages and article for blog posts.
  • og:url: the canonical URL of the page, absolute.
  • og:image: an absolute HTTPS URL to the preview image.
  • og:description: one or two sentences. You can reuse your meta description.

Note that Open Graph uses property, not name. Many parsers accept either, but property is what the spec defines.

Get the image right

The image is what makes a preview stand out, and it is the part that most often breaks.

  • Use 1200 by 630 pixels, a ratio of about 1.91 to 1. It crops reasonably on most services.
  • Keep important text away from the edges, because some apps crop to a square.
  • Use PNG or JPEG. Support for WebP and AVIF varies between services, so a JPEG or PNG is the safe choice.
  • Keep the file reasonably small, a few hundred kilobytes at most. Some bots give up on large files.
  • Make sure the URL is public, not behind a login, and not blocked by robots.txt.

You can give the dimensions so services can lay out the card before the image downloads:


_10
<meta property="og:image:width" content="1200">
_10
<meta property="og:image:height" content="630">

Add the X card type

X reads Open Graph tags for the title, description and image, but uses its own twitter:card tag to decide the layout. Without it you get a small summary card or none. summary_large_image gives the wide image layout.

Set them from your framework

If you use a framework, set these tags through its metadata system so they are rendered on the server. In Next.js, for example, export a metadata object or a generateMetadata function with an openGraph field, and Next.js writes the tags for you.


_11
export const metadata = {
_11
title: 'How we cut our build time in half',
_11
openGraph: {
_11
title: 'How we cut our build time in half',
_11
description: 'Three changes that took our CI from 12 minutes to 6.',
_11
url: 'https://example.com/blog/faster-builds',
_11
images: [{ url: 'https://example.com/og/faster-builds.png', width: 1200, height: 630 }],
_11
type: 'article',
_11
},
_11
twitter: { card: 'summary_large_image' },
_11
};

Test, then refresh the cache

Services cache previews, sometimes for days. After you change the tags, use the service's own tool to re-fetch the page. Facebook has the Sharing Debugger and LinkedIn has the Post Inspector. For other apps, adding a harmless query string such as ?v=2 to the URL forces a fresh fetch.

You can check what bots see with curl:


_10
curl -s https://example.com/blog/faster-builds | grep -i 'og:'

Common pitfalls

#
  • Relative image URLs. /og.png does not work. Bots need the full https:// URL.
  • Tags added by JavaScript. Most preview bots do not run scripts, so client-only tags are invisible to them.
  • The same image and title on every page. It works, but every shared link looks identical. Generate a per-page image if you can.
  • Old cached previews. If your fix does not show, it is probably cached. Re-scrape with the service's tool.
  • Images behind authentication or a CDN that blocks bots. Check your firewall or bot protection rules allow known preview crawlers.
  • Missing alt text. Add og:image:alt so the image is described for people using screen readers in apps that support it.
#

Was this resource helpful?