Sub topic 1
How a pasted link becomes a preview card
When you paste a URL into a chat app or social feed, the app often replaces the bare link with a card showing a title, an image, and a short description. This chapter explains how that card gets built. It covers the sequence of requests, what the platform reads from your HTML, and where the process commonly fails.
Prerequisites
This chapter assumes you know the following:
- Basic HTML structure, including the
headandbodyelements. - What a URL is, and the difference between an absolute URL (
https://example.com/a.jpg) and a relative URL (/a.jpg). - Basic HTTP ideas: a request goes out, a response comes back with a status code, headers, and a body.
You don't need to write server code for this chapter. Later chapters in the tutorial cover generating tags in code.
What a link preview is
A link preview (also called an unfurl) is a summary card that a platform builds for a URL. Slack, Discord, WhatsApp, LinkedIn, Facebook, X, and iMessage all do this. The card usually has four parts:
- A title.
- A short description.
- An image.
- The site's domain or name.
The platform does not guess these from your page by looking at it like a person would. It fetches the HTML, looks for specific metadata you put there, and uses that. If the metadata is missing, it falls back to weaker signals such as the <title> element, and sometimes it shows a bare link.
The most widely supported metadata format is the Open Graph protocol. Facebook created it, and many other platforms read it. Open Graph describes a page as a small set of key-value pairs placed in <meta> tags in the document head.
The pipeline from paste to card
Several steps happen between pasting a link and seeing a card. Knowing the order helps you debug, because each step can fail on its own.
Step 1: The platform requests your URL
A server-side program called a crawler (or scraper, or bot) sends an HTTP GET request to the pasted URL. It does not run in the user's browser, and it does not use the user's cookies or login. Your server sees a request from the platform's IP range with a distinctive User-Agent header, for example facebookexternalhit/1.1 for Facebook or Twitterbot for X.
This has consequences:
- A page that requires login returns a login page or a redirect to the crawler, so the preview shows the login page's metadata or nothing.
- A firewall, bot filter, or
robots.txtrule that blocks the crawler blocks the preview. - The crawler follows redirects, usually a limited number of them, and uses the final page's tags.
Step 2: The platform reads the HTML
The crawler downloads the response body and parses the HTML. It looks inside <head> for <meta> elements whose property attribute starts with og:. Most crawlers read only the first part of the document, so tags buried deep in a huge page, or placed late in the body, may be missed.
Most crawlers don't run JavaScript. They read the HTML your server sends, not the page as it looks after a browser executes your scripts. A single-page app that injects meta tags with client-side code will often produce an empty preview. Chapter "Social crawlers and server-rendered HTML" covers this in detail.
Step 3: The platform fetches the image
The og:image value is a URL, not the image itself. The crawler makes a second request to that URL, checks the file size and format, and may resize or crop it. If the image URL is relative, blocked, too large, or returns an error, the card renders without an image or with a fallback.
Step 4: The platform caches the result
The platform stores the extracted data. Later shares of the same URL reuse the stored card instead of fetching the page again. This is why editing your tags often seems to have no effect: the platform is showing the old data. Cache lifetimes differ by platform and are mostly not documented, and they can range from hours to weeks. Chapter "Testing previews and refreshing caches" shows how to force a refresh.
Step 5: The card renders
When someone shares the link, the platform builds the card from its cached data and its own layout rules. Layout belongs to the platform, so the same tags can look different in Slack, LinkedIn, and WhatsApp. Your tags supply the content, and the platform decides how to display it.
What the crawler sees
A minimal page that produces a preview looks like this:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>How to Brew Cold Coffee</title>
<meta property="og:title" content="How to Brew Cold Coffee">
<meta property="og:type" content="website">
<meta property="og:url" content="https://example.com/cold-coffee">
<meta property="og:image" content="https://example.com/images/cold-coffee-1200x630.jpg">
<meta property="og:description" content="A simple method for smooth cold-brew coffee at home.">
</head>
<body>
<h1>How to Brew Cold Coffee</h1>
</body>
</html>Each tag has two attributes. property names the Open Graph field. content holds the value. Note that Open Graph uses property, while ordinary HTML metadata such as description uses name. Many crawlers accept either, but the specification says property, so use it.
The image URL is absolute, meaning it includes the scheme and host. The crawler fetches it from outside your site and has no base URL to resolve a relative path against.
Seeing what a crawler receives
You can reproduce step 1 and step 2 yourself. Send a request with a crawler-like user agent and filter the output for og: tags:
curl -s -A "facebookexternalhit/1.1" https://example.com/cold-coffee | grep -i 'property="og:'The -s flag hides the progress meter. The -A flag sets the User-Agent header. If this prints your tags, the HTML contains them without JavaScript. If it prints nothing but your browser's developer tools show the tags, they are being added by client-side code and most crawlers will not see them.
Some sites return different content depending on the user agent. Comparing the output with and without -A can reveal a bot filter or a server that serves different HTML to crawlers.
Where each piece of data comes from
The platform picks each field from a chain of sources, using the first one it finds. The exact order varies by platform, but a typical pattern is:
| Card field | Preferred source | Common fallbacks |
|---|---|---|
| Title | `og:title` | `<title>` element |
| Description | `og:description` | `<meta name="description">` |
| Image | `og:image` | Often none, or a favicon-sized icon |
| Link target | `og:url` | The URL that was fetched |
| Site name | `og:site_name` | The domain name |
Fallbacks are unreliable. A page with only a <title> may get a text-only card, and some platforms show nothing. Relying on explicit Open Graph tags gives you control over the result.
Common failure points in the pipeline
Each step in the pipeline has typical problems. Mapping a symptom to a step makes debugging faster.
| Symptom | Likely step | Typical cause |
|---|---|---|
| No card at all | Request | Crawler blocked, page needs login, server error or timeout |
| Card with wrong title | Parse or cache | Tags rendered by JavaScript, or old data in cache |
| Card without image | Image fetch | Relative image URL, image blocked or too large, HTTP error on the image |
| Old content after an edit | Cache | The platform has not refetched the page |
| Different cards on different platforms | Render | Each platform has its own layout and fallback rules |
A note on `og:url`
The og:url value is the canonical address of the page. When shares of several URL variants, such as ones with tracking parameters, point to the same page, platforms use og:url to group them. If og:url is wrong, for example pointing at your home page on every page, the platform may show the home page's card for all your links. Set it to each page's own canonical URL.
A note on redirects and short links
If a shared URL redirects, for example a short link that sends users to a longer address, the crawler follows the redirect and reads the destination. Long redirect chains, redirects that depend on cookies, and redirects into a login flow can all break the preview. Where possible, share the final URL.
Putting the model together
Treat link previews as a small publishing contract between your page and a third party:
- You publish metadata in the HTML your server returns, with no JavaScript required.
- The platform fetches it as an anonymous bot, reads the tags, then fetches the image.
- The platform stores what it found and reuses it until the cache expires.
- The platform draws the card in its own style.
Most preview problems come from a break in this contract. The crawler cannot reach the page, the tags are not in the HTML, the image cannot be fetched, or the cache holds old data. The rest of this tutorial works through each part: which properties to set, how to prepare images, how Twitter/X cards fit in, how to generate tags for many pages, and how to refresh caches.
Exercises
Exercise
A teammate adds Open Graph tags to a single-page app using a JavaScript function that runs after the page loads. In the browser's developer tools the tags are visible, but pasting the URL into a chat app shows no preview. Explain why, and name one way to confirm it.
Show answer
Most crawlers fetch the raw HTML and do not run JavaScript, so they never see tags that are added by client-side code. To confirm, run curl -s -A "facebookexternalhit/1.1" <url> | grep -i 'property="og:'. If it prints nothing, the tags are not in the server's HTML. The fix is to include the tags in the HTML the server sends, for example through server-side rendering or pre-rendering.
Exercise
You change og:title on a live page and re-share the link, but the card still shows the old title. Which step of the pipeline is most likely responsible, and what should you check first?
Show answer
The caching step. The platform stores the data from its earlier fetch and reuses it until the cache expires or you force a refresh. First confirm with curl that your live HTML contains the new title, then use the platform's debugging or refresh tool to make it fetch the page again.
Exercise
A card shows the correct title and description but no image. The tag is <meta property="og:image" content="/images/cold-coffee.jpg">. What is wrong?
Show answer
The image URL is relative. The crawler fetches the image as a separate request from outside your site and cannot resolve a relative path reliably. Use an absolute URL such as https://example.com/images/cold-coffee.jpg, and make sure the image is publicly reachable.