This site is not affiliated with or endorsed by Cloudflare, Inc. It showcases deployable experiments across most Cloudflare products.

Blog18 min read

Extract website metadata and Open Graph tags at the edge

Deep HTMLRewriter guide for OG/meta extraction on Cloudflare Workers - vs Browser Rendering, JSON-LD, caching, link previews, and Metadata Extractor.

Laptop showing charts and analytics on a glass desk
Photo by Carlos Muza on Unsplash

Link previews, SEO audits, and agent crawls often need title, description, canonical, and Open Graph tags. Fetching HTML and walking it with HTMLRewriter on a Worker is fast and cheaper than launching Browser Rendering for every URL.

The Metadata Extractor experiment returns those fields as JSON with validated http(s) input.

Cloudflare products in this pattern

ProductRoleDocs
WorkersFetch + API facadeEdge compute
HTMLRewriterStreaming CSS-selector HTML parseRuntime API
FetchRetrieve origin HTMLFetch
Browser RenderingEscalate when tags need JSHeadless Chrome
Cache APICache successful metadata JSONCache
KV / R2Longer-lived preview storageStorage

Fetch + rewrite vs headless Chrome

ApproachBest whenProduct
fetch + HTMLRewriterTags exist in initial HTMLHTMLRewriter
Browser RenderingTags injected only after JSBrowser Rendering

Most marketing sites and blogs still ship og:* in the first response. Start cheap; escalate only when fields are missing. Official HTMLRewriter guide: HTMLRewriter · Workers.

What to extract

A practical metadata payload:

  • title - <title> or og:title
  • description - meta description or og:description
  • canonical - rel=canonical or og:url
  • image - og:image
  • siteName - og:site_name
  • Optional: Twitter card tags, theme color, robots meta

Normalize absolute image URLs against the page origin. Truncate oversized descriptions for UI cards. Prefer UTF-8 decoding awareness for international sites.

HTMLRewriter sketch

HTMLRewriter streams the response so you never buffer megabytes just to read a few tags:

rewriter.on('meta[property="og:title"]', {
  element(el) {
    title = el.getAttribute("content") ?? title;
  },
});

Handlers run as the HTML streams - see element handlers in the HTMLRewriter docs. The HTML Rewriter experiment isolates the API itself if you want a smaller learning surface.

Compose richer page APIs

GoalExperimentProduct
Core OG/metaMetadata ExtractorHTMLRewriter
Broader structured JSONWebsite to APIHTMLRewriter
LLM-friendly exportWebsite to llms.txtFetch + parse
JSON-LD entitiesJSON-LD ExtractorHTMLRewriter / JSON
Visual fallback imageScreenshot APIBrowser Rendering

A link-preview product might call metadata first, then optionally screenshot when an image is missing.

Caching strategy

Metadata for a URL is often stable for hours:

  1. Build a cache key from canonical URL
  2. caches.default.match (Cache API)
  3. On MISS, fetch + rewrite, then put with s-maxage
  4. For editorial previews, also store in KV with explicit purge on publish webhooks

Deploy and compare

  1. Read HTMLRewriter
  2. Deploy Metadata Extractor
  3. Point it at a blog post and a JS-heavy SPA
  4. If SPA tags are empty, retry with Browser Rendering scrape / screenshot
  5. Cache successful payloads

You get previews that stay cheap on the common path and only spend browser time when HTML is not enough - Workers as the vehicle, HTMLRewriter as the product capability.

Production checklist

  1. Validate URLs and block private destinations.
  2. Prefer HTMLRewriter first; escalate to Browser Rendering only on miss.
  3. Normalize absolute image URLs.
  4. Cache successful payloads.
  5. Respect robots/ToS for third-party targets.
  6. Store preview cards with explicit TTLs.

FAQ

Why is og:image empty on SPAs?

Client-rendered meta tags never appear in the first HTML. Use Browser Rendering or ask the site for SSR.

JSON-LD instead of OG?

Many sites expose Product/Article entities in JSON-LD - see JSON-LD Extractor. Use both when building rich previews.

Related experiments

These deployable references implement the patterns in this article. Open the docs for API details, then Deploy or clone the source.

  • Website Metadata Extractor - Extract title, description, Open Graph, Twitter Card, and canonical URL from any webpage - SEO metadata API
  • Website to API - Turn any webpage into structured JSON (title, headings, links, images) - HTML-to-API scraper for agents and apps
  • Website to llms.txt - Convert any webpage into llms.txt format for LLM and AI-agent consumption - AI-readable site summary generator
  • HTML Rewriter - Extract HTML stats and transform pages with Cloudflare HTMLRewriter - streaming HTML parse/transform at the edge
  • JSON-LD Extractor - Extract Schema.org JSON-LD structured data from any webpage at the edge - rich results and SEO markup inspection
  • Screenshot API - Capture website screenshots with Cloudflare Browser Rendering - full-page or viewport PNG/JPEG screenshot API

Cloudflare Experiments is an independent open-source catalog - not affiliated with Cloudflare, Inc.