Sub topic 9
Testing Previews and Refreshing Caches
This chapter assumes you can write the core Open Graph tags from earlier chapters, read raw HTTP responses with curl, and run a short Node.js script (Node 18 or newer). Writing tags is the easy part. The hard part is confirming that a crawler sees what you intended, and then getting a platform to forget the stale copy it saved earlier.
Why previews need their own testing
A preview card is not built in your browser. The platform's crawler fetches your page from its own servers, reads the tags, stores the result, and reuses that stored result for later shares. Three things follow from this:
- The crawler may see a different page than you do. It does not log in, it usually does not run JavaScript, and it may be blocked by a firewall.
- The first share fixes what everyone sees afterward. Fixing a tag on your site does not change the stored copy.
- Each platform keeps its own cache, with its own rules and its own tools.
The diagram shows the flow you are debugging.
When a preview is wrong, the problem is in one of three places: what your server returned, what the crawler stored, or how the card was drawn. Test them in that order.
Step 1: Check what the server returns
Start with curl, because it shows the raw response with no JavaScript and no browser extensions.
curl -s -L -A "facebookexternalhit/1.1" https://example.com/cold-coffee | grep -i 'property="og:'The flags matter:
-shides the progress meter.-Lfollows redirects, as crawlers do.-Asets the user agent. Some servers return different HTML to bots, for example a bot-detection page or a pre-rendered version.
If this prints nothing, the tags are missing from the initial HTML. A common cause is a single-page app that injects <meta> tags after the page loads. Crawlers that don't execute JavaScript never see them. The fix is server-side rendering or a pre-render step, not a debugger button.
Check the status code and redirect chain too:
curl -s -o /dev/null -L -A "Twitterbot/1.0" -w "%{http_code} %{url_effective}\n" https://example.com/cold-coffeeThis prints the final status and the final URL. You want 200. A 403 or 429 means the crawler is being blocked. A 301 to a login page means the preview will show the login page's tags.
Compare user agents
Crawlers announce themselves with distinct user-agent strings. You can use these to reproduce what each platform receives.
| Platform | Crawler token | Where to check |
|---|---|---|
| Facebook, Messenger, Instagram | `facebookexternalhit`, `Facebot` | Sharing Debugger |
| `LinkedInBot` | Post Inspector | |
| X (Twitter) | `Twitterbot` | Composer preview |
| Slack | `Slackbot-LinkExpanding` | Paste in a private channel |
| Discord | `Discordbot` | Paste in a private channel |
If your server serves different content to these tokens, test each one with curl -A. Platforms change their tools over time, so treat this table as a starting point and confirm in each platform's current documentation.
Step 2: Automate the check
Running curl | grep by hand misses the problems that break most previews: relative image URLs, images that return 404, and missing core properties. This script checks all three. It uses fetch, which is built into Node 18 and newer.
const CRAWLER_UA = "facebookexternalhit/1.1";
const REQUIRED = ["og:title", "og:type", "og:image", "og:url"];
function extractOgTags(html) {
const tags = {};
for (const tag of html.match(/<meta\s+[^>]*>/gi) ?? []) {
const prop = /property=["']([^"']+)["']/i.exec(tag);
const content = /content=(?:"([^"]*)"|'([^']*)')/i.exec(tag);
if (prop && content && prop[1].startsWith("og:") && !(prop[1] in tags)) {
tags[prop[1]] = content[1] ?? content[2];
}
}
return tags;
}
async function checkPreview(pageUrl) {
const res = await fetch(pageUrl, {
headers: { "User-Agent": CRAWLER_UA },
redirect: "follow",
});
console.log(`Page: ${res.status} ${res.url}`);
const tags = extractOgTags(await res.text());
for (const name of REQUIRED) {
console.log(`${tags[name] ? "ok " : "MISSING"} ${name} ${tags[name] ?? ""}`);
}
const image = tags["og:image"];
if (!image) return;
if (!/^https?:\/\//i.test(image)) {
console.log("WARN og:image is not an absolute URL");
return;
}
const imgRes = await fetch(image, {
headers: { "User-Agent": CRAWLER_UA },
redirect: "follow",
});
const bytes = (await imgRes.arrayBuffer()).byteLength;
console.log(
`Image: ${imgRes.status} ${imgRes.headers.get("content-type")} ${bytes} bytes`
);
}
checkPreview(process.argv[2]).catch((err) => {
console.error("Request failed:", err.message);
process.exit(1);
});Run it with node check-preview.js https://example.com/cold-coffee.
The extractor is a regular expression, so it is a quick check and not a full HTML parser. It records only the first occurrence of each property. That mirrors the usual crawler behavior for single-value properties, and it makes duplicated tags easy to notice if you print them.
Step 3: Check what the platform stored
A correct response from your server does not prove the platform has the new version. Use the platform's own tools to see the stored result and to force a new fetch.
Facebook Sharing Debugger
The Sharing Debugger at developers.facebook.com/tools/debug/ shows what Facebook last scraped, the HTTP status it received, any warnings, and the image it chose. Two buttons matter:
- Debug shows the cached scrape, or scrapes if nothing is cached.
- Scrape Again discards the cached copy and fetches the page fresh.
If the page changed and the card didn't, click Scrape Again. You may need to click it twice, because the first click can display the previous result while the new one is processed. The tool also lists fields it inferred. A warning such as "inferred property" means a tag was missing and Facebook guessed.
LinkedIn Post Inspector
LinkedIn's Post Inspector at linkedin.com/post-inspector/ fetches the URL when you inspect it, which also refreshes LinkedIn's cache for that URL. Sign in, paste the URL, and read the title, image, and any errors.
X, Slack, and Discord
X retired its standalone Card Validator. The practical test is to paste the URL in the post composer and check the preview. You do not have to publish the post. X decides when to refetch, and you have little control over it.
Slack and Discord have no public debugger. Test by pasting the link into a private channel or direct message. Slack caches the expanded card per URL, so a repeat paste may show the old card even after you fix the page.
Step 4: Refresh stale caches
When you cannot force a re-scrape, you have three options. Choose by how much you control.
- Use the platform's refresh tool. This works for Facebook and LinkedIn and is the cleanest option because the URL stays the same.
- Change the image URL when the image changes. Platforms cache the image by its URL. Serving a new file at the same URL leaves the old image in the card. Publish
cold-coffee-v2-1200x630.jpgor add a version in the path, and updateog:image. - Change the page URL for a new share. Adding a query string such as
?v=2makes the platform treat the link as new. This works on platforms with no refresh tool, such as Slack. It also splits share counts and analytics across URLs unless you setog:urlto the clean canonical address.
Cache lifetimes vary by platform and change without notice. Some keep an entry for days or weeks. Plan for previews to be stale at first, and do your testing before you publish, not after.
Step 5: Test before the page is public
Crawlers cannot reach localhost, a staging site behind a login, or a preview deploy protected by a password. For those, you have two choices:
- Run the
curland script checks against the local or staging URL to verify the HTML. These need no platform access. - Expose the page temporarily through a tunnel or an unprotected preview URL, then use the platform tools against that address.
Be careful with tunnels. A tunnel gives a public URL to whatever you serve, so use it only for pages that are safe to expose, and close it afterward.
A useful release habit is to run the script from Step 2 in your deployment pipeline against the production URL right after deploy. A failure for a missing og:image or a 404 image costs seconds and avoids a bad first share.
Reading the results: a diagnosis table
Use what you observe to pick the likely cause.
| Symptom | Likely cause | Check |
|---|---|---|
| No tags in the `curl` output | Tags injected by JavaScript | View source, not DevTools Elements |
| Debugger reports a `403` or `429` | Firewall or bot protection blocks the crawler | Server logs for the crawler user agent |
| Card shows the old title | Platform cache | Scrape Again or Post Inspector |
| Card shows the new title but the old image | Image cached by URL | Use a new image filename |
| Text appears but no image | Relative or unreachable `og:image` | The image check in the script |
| Wrong URL under the card | `og:url` differs from the shared URL | Compare `og:url` and the final URL |
| Tags are ignored entirely | `name=` used instead of `property=` | Look at the attribute in the source |
Common mistakes when testing
- Testing only in a browser. Your browser runs JavaScript and sends cookies. The crawler does neither.
- Fixing the page and not the cache. Always refresh through the platform tool after a change.
- Overwriting the image at the same URL. The old image keeps appearing. Version the filename.
- Forgetting the redirect. If
http://redirects tohttps://or awwwhost,og:urland your shared link should match the final address. - Checking one platform only. A card correct on Facebook can still be wrong on Slack, because each platform has its own cache and its own parser.
Exercise
You changed the og:image file on your server, keeping the same URL https://example.com/images/cover.jpg. The Facebook Sharing Debugger still shows the old picture after you click Scrape Again. What is the most reliable fix, and why?
Show answer
Publish the new image at a new URL, such as https://example.com/images/cover-v2.jpg, update og:image to point to it, then scrape again. Platforms cache images by URL, so a file replaced at the same address can keep serving the stored copy. A new URL cannot match any cached entry.
Exercise
curl -s -A "facebookexternalhit/1.1" https://example.com/post | grep -i 'property="og:' prints nothing, but the tags appear when you inspect the page in your browser's DevTools. What is the likely cause, and what should you change?
Show answer
The tags are added by JavaScript after the page loads. The crawler reads only the initial HTML, so it never sees them. Render the tags on the server, or pre-render the page, so they are in the HTML response.
Exercise
A teammate shares https://example.com/post?v=2 to force a refresh in Slack. Name one downside, and the tag that limits it.
Show answer
The link is now a separate URL from the original, so share counts and analytics are split between the two. Setting og:url to the clean canonical URL (https://example.com/post) keeps platforms that honor it consistent about which address is the real one.