Link Unfurl Service with Screenshots: The Architecture That Works
Learn how to build a link unfurl service with screenshots — from URL parsing to caching, fallback previews, and rendering with a screenshot API.
Paste a link into Slack, Discord, or a chat product you built, and a preview card appears — title, description, and an image. That's link unfurling. When the target page has no Open Graph image (or a broken one), the fallback most teams reach for is a screenshot of the page itself. This post walks through building that system end to end.
What a Link Unfurl Service Actually Does
When someone pastes a URL, your service needs to:
- Fetch the URL and extract metadata (title, description, OG tags)
- Determine if a usable preview image already exists
- If not, generate one — typically via screenshot
- Cache the result so you're not re-fetching on every paste
- Return a structured payload (JSON) that your frontend renders as a card
The tricky part isn't the happy path — it's handling the pages that don't play nice: JS-heavy SPAs, pages behind auth walls, slow-loading sites, and infinite scroll pages that never "finish" loading.
Why Screenshots Instead of Just Scraping Meta Tags
Scraping og:image tags is the first step, but it fails more often than you'd expect:
- Many sites simply don't set
og:image - Some set it to a generic logo that looks bad at small sizes
- Client-side rendered pages (React, Vue) often have empty meta tags in the raw HTML your scraper fetches
- Images can 404 or point to relative paths that break outside the original domain
A screenshot is a guaranteed fallback — it renders whatever's actually visible on the page, JS included, so it works even when metadata is missing or wrong.
Step 1: Build the Metadata Extraction Layer
Start with a lightweight fetch-and-parse step before touching screenshots at all — it's cheaper and faster.
- Fetch the target URL with a short timeout (3-5 seconds)
- Parse the HTML with something like
cheerio(Node) orBeautifulSoup(Python) - Extract
og:title,og:description,og:image, and fall back to<title>and the first<meta name="description"> - Validate the
og:imageURL actually resolves (HEAD request, check content-type is an image)
If og:image exists and resolves, you're done — no screenshot needed. This alone will cover 40-60% of URLs depending on your traffic mix.
Step 2: Trigger a Screenshot for the Rest
For everything else, call a screenshot API to render the actual page. This is where a service like PxShot fits in — instead of running headless Chrome yourself, you send a URL and get back an image.
A typical request looks like:
GET https://api.pxshot.dev/v1/screenshot?url=https://example.com&format=jpeg&width=1200&height=630
A few parameters matter more than others for unfurl use cases:
- Dimensions: match the aspect ratio your chat client expects — 1200x630 is the de facto standard for OG images
- Format: JPEG or WebP for smaller payloads; PNG only if you need transparency (rare for full-page shots)
- Wait conditions: pages with lazy-loaded hero images need a delay or a "wait until network idle" flag before the screenshot fires
- Viewport vs. full page: for unfurl previews, capture just the visible viewport — full-page screenshots look wrong when squeezed into a small card
- Set a hard timeout (8-10 seconds) on the screenshot request itself
- If the screenshot fails, fall back to a generated placeholder card with just the title/domain
- Log failure rates by domain — some sites will consistently block headless browsers via bot detection, and you'll want to blocklist rather than retry them forever
- Cache by normalized URL — strip tracking params (
utm_*,fbclid) before using the URL as a cache key - Set a TTL of 24-72 hours for most content; news sites and marketing pages don't need re-screenshotting every hour
- Store the image in object storage (S3, R2) rather than re-requesting from the screenshot API each time — even a fast API adds latency you don't need on a cache hit
- Use a stale-while-revalidate pattern — serve the cached image immediately, and kick off a background refresh if the TTL has expired
Handling Slow or Broken Pages
Not every URL will render cleanly. Build in defensive handling:
Step 3: Caching Strategy
This is where most homegrown unfurl services fall apart at scale. Without caching, every paste of the same URL re-triggers a screenshot, which is slow and wasteful.
A simple cache table:
{
"url_hash": "a1b2c3...",
"title": "Example Page",
"description": "...",
"image_url": "https://cdn.yourapp.com/cache/a1b2c3.jpg",
"source": "screenshot" | "og_tag",
"created_at": "2024-01-15T10:00:00Z",
"expires_at": "2024-01-17T10:00:00Z"
}
Step 4: Rendering the Preview Card
Once you have title, description, and image, the frontend rendering is straightforward — but a few details separate a good unfurl from a janky one:
- Truncate titles at ~70 characters and descriptions at ~160 to avoid overflow
- Show a domain label (e.g. "example.com") below the title so users know where the link goes
- Use a skeleton loader while the preview is being generated, rather than a blank card
- If the screenshot generation is still in progress on first paste, return the metadata immediately and stream the image in once it's ready — don't block the whole card on the slowest part
Common Mistakes to Avoid
- Screenshotting every URL unconditionally — always check for a valid OG image first; screenshots should be the fallback, not the default
- No rate limiting on user-submitted URLs — someone will paste a link to your own screenshot endpoint in a loop if you let them
- Ignoring redirects — resolve the final URL before caching, otherwise you'll cache previews under the wrong key
- Fixed timeouts too short for JS-heavy sites — a 2-second timeout will produce blank white screenshots for a lot of modern SPAs
- Not respecting robots.txt or rate limits on target sites — aggressive unfurling can get your IP blocked by the sites you're trying to preview
Scaling Beyond a Side Project
If you're running this at chat-app scale — thousands of unique URLs pasted per day — running your own headless Chrome fleet becomes a real operational cost: memory leaks, Chrome crashes, keeping browser versions patched, and scaling workers up and down with load. This is the exact problem a hosted screenshot API removes from your plate. Instead of managing browser infrastructure, you send a request to PxShot and get PNG, JPEG, WebP, or PDF back, with the rendering, retries, and browser lifecycle handled on their end.
It also means your unfurl service's reliability isn't tied to your ability to keep a Chromium binary alive on a cron-managed EC2 box at 3am.
A Minimal Implementation Checklist
- URL normalization and redirect resolution
- Meta tag extraction with a fast HTML parser
- Screenshot fallback via API call
- Object storage for generated images
- Cache layer with TTL and stale-while-revalidate
- Domain-level failure tracking and blocklisting
- Rate limiting on the ingest endpoint
If you want to prototype this without standing up headless Chrome yourself, PxShot has a free tier that's enough to build and test the whole pipeline above before you commit to anything.