Link previews in Cloudflare Workers
Extract Open Graph and URL metadata in a Cloudflare Worker with linkpeek: fetch-native, no Node APIs, with cache headers and safe defaults.
linkpeek uses platform fetch, Web Streams, TextDecoder, and AbortController. It needs no Node HTTP client or DOM implementation.
Create a Worker endpoint
npm install linkpeekimport { preview } from "linkpeek";
export default {
async fetch(request: Request): Promise<Response> {
const url = new URL(request.url).searchParams.get("url");
if (!url) return Response.json({ error: "Missing url" }, { status: 400 });
try {
const result = await preview(url);
return Response.json(result, {
headers: {
"Cache-Control": result.statusCode >= 200 && result.statusCode < 300
? "public, max-age=3600"
: "no-store",
},
});
} catch {
return Response.json({ error: "Preview unavailable" }, { status: 422 });
}
},
};Caching
The Cache-Control header lets browsers and downstream caches reuse a result. This Worker doesn't use the Cloudflare Cache API, so every request that reaches the handler still fetches the target. If that matters, add a shared cache keyed on a stable URL, store only 2xx previews, and rate-limit public callers.
Runtime limitations
linkpeek validates literal addresses and each redirect target. The Worker runtime resolves hostnames and establishes connections. Keep private-IP access disabled and use platform egress controls when hostile public input is in scope.
React
Build an accessible React link preview card: a server endpoint calls linkpeek, and the card renders title, description, image and host with fallbacks.
Node.js + Express
Build a link preview and URL metadata API with linkpeek, Node.js and Express: input validation, caching, error handling and safe defaults.