Sub topic 8
Social Crawlers and Server-Rendered HTML
This chapter assumes you know basic HTML structure, the difference between absolute and relative URLs, and how HTTP requests and responses work. It also assumes you have seen Open Graph meta tags in a page's <head>. Here you will learn who reads those tags and why they must be in the HTML your server sends.
Who reads your tags
When someone pastes a URL into a chat or social app, the platform does not open your page in a browser. A program called a social crawler fetches the URL from the platform's servers. It reads the HTML, extracts the og: tags, and builds the preview card. Examples of crawler user agents include facebookexternalhit (Facebook), Twitterbot (Twitter/X), LinkedInBot, Slackbot, and WhatsApp.
These crawlers are simple. They send one HTTP GET request, read the response body, and parse it. Most of them do not run JavaScript, wait for network calls, or log in to your site.
Why server-rendered HTML matters
Suppose your site is a single-page application. The server sends a nearly empty HTML file, and JavaScript fills in the title and tags after the page loads in a browser. A person sees a correct page. A crawler sees only the empty shell.
Compare the two responses:
| Approach | What the crawler receives | Preview result |
|---|---|---|
| Server-rendered or statically generated HTML | Full `<head>` with `og:` tags | Correct title, description, and image |
| Client-side rendering only | Generic shell, tags added later by JavaScript | Missing or generic preview |
The rule is simple: the og: tags must be present in the first HTML response. Tags added to the DOM by scripts after load are invisible to most crawlers.
If you use a client-side framework, choose one of these fixes:
- Server-side rendering (SSR), where the server builds the full HTML for each request.
- Static generation, where HTML files with tags are built ahead of time.
- Prerendering for crawlers only, where the server detects crawler user agents and returns a pre-built version.
The first two are preferred. Serving different content by user agent is easy to get wrong and can drift from what users see.
See what the crawler sees
You can check your page without running JavaScript. Fetch it with a crawler-like user agent and filter for og: tags:
curl -s -A "facebookexternalhit/1.1" https://example.com/cold-coffee | grep -i 'property="og:'If this prints your tags, a crawler can read them. If it prints nothing but the tags appear in the browser's developer tools Elements panel, they are being added by JavaScript. Use "View Source" in the browser to see the raw HTML response, which is closer to what the crawler gets. The Elements panel shows the page after scripts have run.
Other things crawlers need
The tags can be correct and the preview can still fail. Check these points:
- Public access. The page must load without a login, a cookie, or a session. A crawler that is redirected to a sign-in page will read the sign-in page's tags.
- No bot blocking. Firewalls, CDN bot protection, or
robots.txtrules that block crawler user agents will stop the fetch. Allow the crawlers of the platforms you care about. - Fast response. Crawlers time out. If your server takes many seconds to respond, the preview may be empty.
- Early tags. Some crawlers read only the first part of the document. Put the
og:tags near the top of<head>, before large inline scripts or styles. - Absolute URLs. The crawler has no page context, so
og:imageandog:urlmust be full URLs such ashttps://example.com/images/cold-coffee-1200x630.jpg. - Reachable image. The image URL is fetched separately and must also be public.
Exercise
Your React app sets og:title with JavaScript after the page loads. In a browser, DevTools shows the tag, but the shared link preview is generic. Why, and what is one fix?
Show answer
Most social crawlers fetch the raw HTML and do not run JavaScript, so they never see the tag that the script adds. Fix it by putting the og: tags in the HTML the server sends, using server-side rendering, static generation, or prerendering. You can confirm with curl -s -A "facebookexternalhit/1.1" URL | grep -i 'property="og:', which should then print the tags.
Exercise
A page has correct og: tags in its source, but a crawler shows the tags of your login page. Name two possible causes.
Show answer
The page may require authentication, so the crawler is redirected to the login page. A bot-protection rule or redirect may also send crawler user agents to a different page. Make the shared URL publicly reachable and allow the crawler's user agent.