Sub topic 1
How a Pasted Link Becomes a Preview Card
Prerequisites
This chapter assumes you know the following:
- Basic HTML structure, including the
headandbodyelements. - The difference between absolute URLs (
https://example.com/page) and relative URLs (/page). - Basic HTTP requests and responses, such as what a status code and a request header are.
You do not need to know Open Graph yet. This chapter builds the working model that the rest of the tutorial relies on.
What a link preview is
When you paste a URL into a chat app or a social feed, the app often replaces the bare link with a card. The card usually has an image, a title, and a short description. This card is a link preview.
The platform does not guess these values. It reads them from your page. Your job as a site owner is to put the right data in the HTML so the card looks the way you want.
The steps behind a preview
A preview is built in a few steps. The platform runs them, not the user's device.
- A user pastes or posts a URL.
- The platform's server sends an HTTP request to that URL. The software that does this is called a crawler. A crawler is a program that fetches web pages automatically.
- The server returns HTML. The crawler reads the
headof the document and looks formetatags. - The platform picks out values such as title, image, and description.
- It may download the image, resize it, and store a copy.
- It saves the result in a cache, which is a stored copy reused for later requests.
- It shows the card to the user, and to anyone else who shares the same URL later.
Two details in this flow matter later. First, the crawler is not a person's browser, so it does not behave like one. Second, the cache means the platform often does not fetch your page again when the link is shared a second time.
Where the data comes from
The crawler reads meta tags in the document head. A meta tag is an HTML element that carries information about the page instead of visible content. Here is a small example:
<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>The og: prefix stands for Open Graph. Open Graph is a protocol, meaning an agreed set of names and rules, that lets a page describe itself to other services. Facebook introduced it, and many platforms now read it. The next chapters cover each property in detail.
Notice that the image and page URLs are absolute. The crawler requests your page from outside your site, so a relative path like /images/cold.jpg has no reliable base to resolve against. Always write the full https:// address.
What happens without tags
If a page has no Open Graph tags, most platforms fall back to other signals. They may use the title element and the meta name="description" tag. They may pick an image from the page body, or show no image at all. The result is unpredictable, and it differs from one platform to another. Writing the tags yourself removes that guesswork.
See what a crawler sees
You can fetch your page the way a crawler does. The -A option of curl sets the user agent, which is the name a client sends to identify itself. The command below pretends to be Facebook's crawler and filters for Open Graph lines:
curl -s -A "facebookexternalhit/1.1" https://example.com/cold-coffee | grep -i 'property="og:'If this prints your tags, a crawler can see them. If it prints nothing, the tags are missing from the HTML the server sends.
Why caching matters now
Because the platform stores the result, editing your tags does not change an existing preview right away. You may see the old title or image for hours or days. Later chapters show how to test previews and ask platforms to refresh them. For now, remember that a preview shows what the crawler saw at the time it last fetched the page, not what your page says today.
Exercise
You add Open Graph tags to a page with JavaScript after the page loads. In your browser the tags appear in the developer tools, but a platform shows no preview image. Give one likely cause and one way to check it.
Show answer
The crawler reads only the HTML the server returns and does not run JavaScript, so it never sees the tags added by the script. Check by running curl with a crawler user agent, for example curl -s -A "facebookexternalhit/1.1" URL | grep -i 'property="og:'. If nothing prints, the tags are not in the server's HTML. Fix it by rendering the tags on the server.
Exercise
Why should the og:image value be an absolute URL, and what could go wrong if the platform has cached an older preview?
Show answer
The crawler fetches the page from outside your site, so a relative path has no reliable base and the image may not load. Use the full https:// address. If a preview is cached, the platform may keep showing the old title or image after you edit the tags, until the cache expires or is refreshed.