Sub topic 8
Social crawlers and server-rendered HTML
This chapter explains how the programs that fetch your page for link previews behave, and why that behavior decides where your Open Graph tags must live. It assumes you know basic HTML, the difference between absolute and relative URLs, and how HTTP requests, responses, status codes and headers work. You should also have read the earlier chapters on the og: core properties and on how a pasted link becomes a preview card.
What a social crawler is
When someone pastes a URL into a chat or social post, the platform does not ask the browser to render the page. A server-side program called a crawler requests the URL, reads the HTML, extracts the meta tags, and stores the result. Every later viewer of that link sees the stored result.
A crawler is closer to curl than to Chrome. It sends an HTTP GET request, follows a limited number of redirects, reads the response body, and parses it. It does not behave like a person with a browser.
Most platforms identify their crawler with a User-Agent header. The names below are the commonly documented ones. Check each platform's documentation, because names and behavior change.
| Platform | Typical user agent token |
|---|---|
| Facebook, Messenger, Instagram | `facebookexternalhit`, `Meta-ExternalAgent` |
| Twitter/X | `Twitterbot` |
| `LinkedInBot` | |
| Slack | `Slackbot-LinkExpanding` |
| Discord | `Discordbot` |
| Telegram | `TelegramBot` |
| `WhatsApp` |
What the crawler does and does not do
The following behaviors hold for most social crawlers. Treat them as working assumptions and confirm them with a test (shown later in this chapter).
- It does not run your JavaScript. Tags that a script inserts into
<head>after page load are invisible to it. - It does not log in. It has no cookies and no session. If the URL redirects to a login page, the preview is built from the login page.
- It reads only the HTML response. It does not wait for API calls, hydration, or timers.
- It follows redirects, but only a few. Long redirect chains, or redirects that depend on cookies, often fail.
- It has timeouts. A page that takes many seconds to respond may produce no preview or a bare link.
- It fetches the image separately. The image URL from
og:imageis requested in a second step, and that request can fail on its own. - It may stop reading early. Some crawlers read only the first part of the body. Put your meta tags near the top of
<head>, before large inline scripts or styles.
Why client-side rendering breaks previews
A single-page application (SPA) often serves the same near-empty HTML file for every route. JavaScript then builds the page in the browser, and may set document.title or add meta tags at that point.
Here is a typical SPA shell:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>My Shop</title>
</head>
<body>
<div id="root"></div>
<script src="/assets/app.js"></script>
</body>
</html>A browser shows a full product page for /products/42. A crawler receives the HTML above and sees one title, My Shop, and no Open Graph tags. The preview is empty or generic, and it is the same for every URL on the site.
Some developers fix this in the client with a library that edits the head after load. That works for Google's rendering pipeline in many cases, but it does not help social crawlers. The tags must be in the HTML that the server sends.
Approaches that put tags in the initial HTML
There are four common ways to get per-page tags into the first response. They differ in cost and in how much of your stack they touch.
| Approach | How it works | Trade-off |
|---|---|---|
| Server-side rendering (SSR) | The server builds the full HTML, including head tags, for each request | Needs a server runtime. Most frameworks support it. |
| Static generation | Pages are rendered to HTML files at build time | Fast and cheap. Content changes need a rebuild. |
| Templating on the server | A traditional framework fills a template with page data | Simple. Works well for content sites. |
| Bot-specific rendering | The server or CDN detects bot user agents and returns pre-rendered HTML to them only | Adds a second code path. It can drift from what users see. |
Prefer the first three. They give crawlers and users the same HTML. With bot-specific rendering you maintain a user-agent list, and a mismatch between the bot version and the user version can violate platform or search-engine rules.
A minimal server-rendered example
This example uses Node.js 18 or later and Express. It has no build step, and the server reads the page data and writes the tags into the HTML before responding. Save it as server.js, then run npm init -y, npm pkg set type=module, npm install express, and node server.js.
import express from "express";
const app = express();
const ORIGIN = "https://example.com";
const posts = {
"cold-coffee": {
title: "How to Brew Cold Coffee",
description: "A simple method for smooth cold-brew coffee at home.",
image: `${ORIGIN}/images/cold-coffee-1200x630.jpg`,
imageAlt: "A glass of cold brew coffee with ice on a wooden table",
},
};
function escapeHtml(value) {
return String(value)
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """);
}
function renderPage(slug, post) {
const url = `${ORIGIN}/${slug}`;
const tag = (property, content) =>
` <meta property="${property}" content="${escapeHtml(content)}">`;
return `<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>${escapeHtml(post.title)}</title>
${tag("og:title", post.title)}
${tag("og:type", "article")}
${tag("og:url", url)}
${tag("og:image", post.image)}
${tag("og:image:alt", post.imageAlt)}
${tag("og:description", post.description)}
<meta name="twitter:card" content="summary_large_image">
</head>
<body>
<div id="root"><h1>${escapeHtml(post.title)}</h1></div>
<script src="/assets/app.js" defer></script>
</body>
</html>`;
}
app.get("/:slug", (req, res) => {
const post = posts[req.params.slug];
if (!post) {
res.status(404).type("html").send("<h1>Not found</h1>");
return;
}
res.type("html").send(renderPage(req.params.slug, post));
});
app.listen(3000, () => console.log("Listening on http://localhost:3000"));Three details matter for crawlers.
- The tags come first in
<head>, ahead of any script or stylesheet. - The script is loaded with
deferat the end of the body. Hydration can still happen for users, but nothing the crawler needs depends on it. - Unknown slugs return a real
404status. A200response with a "not found" message in the body would be cached as a valid preview of a broken page.
The same pattern applies in frameworks. Next.js, Nuxt, SvelteKit, Remix and Astro each have a way to set head tags during server rendering. Look for an API that runs on the server, and verify the output with the test below, because a framework API that only works in the browser will not help.
Test what the crawler sees
You can reproduce the crawler's view with curl. It makes the same kind of request and runs no JavaScript.
curl -s -A "facebookexternalhit/1.1" https://example.com/cold-coffee | grep -i 'property="og:'If this prints your tags, a no-JavaScript client can see them. If it prints nothing, the tags are being added by client-side code, or your server is returning different HTML for this user agent.
Two more checks catch common problems.
# Show the status code and every redirect hop
curl -sIL -A "Twitterbot/1.0" https://example.com/cold-coffee | grep -iE '^(HTTP|location|content-type)'
# Fetch the image the way the crawler will, and confirm it returns 200 and an image type
curl -sI -A "Twitterbot/1.0" https://example.com/images/cold-coffee-1200x630.jpg | grep -iE '^(HTTP|content-type|content-length)'In the output, look for these things:
- A final
HTTP/... 200status for both the page and the image. - A small number of redirect hops. A single
httptohttpsredirect is fine. - A
Content-Typeoftext/htmlfor the page andimage/jpeg,image/pngor similar for the image. - No
Locationheader that points to a login or consent page.
Spoofing the user agent with curl shows you what a bot would receive from your server, but it cannot reproduce everything. Platform crawlers run from known IP ranges, and a firewall or bot-protection service may block them while allowing your curl request. For the final check, use the platform's own debugging tool, which the chapter on testing and cache refresh covers.
Things that stop crawlers from reaching your page
Even with correct tags in the HTML, a crawler can fail to get them. Check these in order.
Blocking at the network or application layer
- Web application firewalls and bot protection. Services that challenge unknown clients can return a CAPTCHA page or a
403to crawlers. Allow the platforms' documented user agents and IP ranges. - Authentication. Pages behind login, VPN, or staging passwords cannot be previewed. Use a public URL, or serve a minimal public page for the preview.
- Geo and cookie consent redirects. A redirect to a consent wall that depends on cookies will send the crawler to the wall every time.
robots.txt. Some crawlers honor it and some do not. If a rule disallows the path, a crawler that respects it will skip the fetch. Do not rely onrobots.txtto hide a page from social crawlers, and do not assume it will always block them.
Problems in the response itself
- Slow responses. Render the head quickly. If the database call for the page takes seconds, the crawler may give up.
- Wrong status codes. Return
200for pages that exist and404for ones that do not. A5xxresponse means no preview. - Huge heads. Large inline scripts or styles before the meta tags can push the tags past the point where the crawler stops reading.
- Compressed or odd encodings. Send
Content-Type: text/html; charset=utf-8and keep the<meta charset>tag within the first bytes of the document, so titles with accents or non-Latin letters are decoded correctly.
Summary
Social crawlers fetch your URL as plain HTTP clients and build the preview from the HTML they receive. They run no JavaScript, hold no session, and give up on slow or redirect-heavy pages. Open Graph tags must therefore be in the server's first response, near the top of <head>, with correct status codes and a reachable image URL. Server-side rendering, static generation, or server templating achieve this directly. Check your result with curl and a crawler user agent before relying on a platform's debugger.
Exercise
Your React single-page app sets og:title with a library that updates document.head after the page loads. In a browser, "View source" shows no og: tags but the Elements panel does. A shared link shows a generic preview. Why, and name two ways to fix it?
Show answer
"View source" shows the HTML the server sent, and the Elements panel shows the DOM after JavaScript ran. Social crawlers read only the server's HTML and do not run JavaScript, so they never see the tags. Fix it by rendering the tags on the server, using server-side rendering or static generation. Alternatively, serve pre-rendered HTML of the same content to known bot user agents.
Exercise
A curl -sIL -A "Twitterbot/1.0" request to your page prints HTTP/2 302, a location: https://example.com/consent, and then HTTP/2 200. Pages are previewed correctly in your browser. What is likely wrong with the preview, and what would you change?
Show answer
The crawler is being redirected to a cookie-consent page because it has no cookies, so the preview is built from the consent page's HTML. Change the server so the redirect does not apply to the page's HTML response, for example by showing the consent prompt as an overlay on the page itself instead of redirecting. Then repeat the curl check and confirm the final response is the real page with its og: tags.
Exercise
A missing article returns status 200 with the text "Sorry, nothing here" and the site's default Open Graph tags. Why is this a problem for previews, and what should the server return?
Show answer
The crawler treats a 200 response as a valid page, so it stores and shows the default tags as the preview for a URL that does not exist, and that result may stay cached. The server should return a 404 status for missing pages. A 404 tells the crawler there is no content to preview.