The handleOGFetch function is a server-side utility that fetches and parses Open Graph metadata from any URL. It takes a standard Request and returns a Response, so it works in any server that speaks the Fetch API, and extracts title, description, image, and favicon information from web pages.
In a Next.js app, use createOgFetchRoute from @once-ui-system/core/next: a two-line route file built on this function. See OG route handlers.
- Fetches HTML pages only, up to a byte limit, within a timeout
- Extracts Open Graph metadata (title, description, image, favicon)
- Falls back to standard HTML meta tags if OG tags aren't available
- Returns image and faviconUrl as absolute URLs, resolved against the page
- Decodes HTML entities in text content
- Supports custom user agents
- Only requests public addresses: private, loopback, link-local and metadata addresses are refused in every encoding, every redirect is checked again, and the connection goes to the address that was checked
- Domain allowlist and blocklist, plus an isAllowed hook for your own checks
- Requires Node.js runtime (resolves and checks hostnames)
Create an API route that uses the utility:
Then fetch metadata from your client:
Customize timeout, user agent, and entity decoding:
The function returns a JSON response with the following structure:
The function returns appropriate error responses:
- 400: Missing URL parameter or URL failed validation (blocked domain, private IP, etc.). A hostname that does not resolve is refused the same way, so the route cannot tell a caller which internal names exist.
- 403: Cross-origin caller, with sameOrigin
- 415: The URL is not an HTML page
- 502: The upstream failed (error status, too many redirects)
- 504: The upstream did not answer within timeout
- 500: Not running on the Node.js runtime
Combine with the useOgData hook for seamless client-side integration: