Reference

Component reference

Attributes, CSS variables, and stable selectors for the preview component.

Load the component once, then use <link-metadata-preview> anywhere you need a card.

<script type="module" src="https://api.linkmetadata.com/component.js" async></script>

<link-metadata-preview data-url="https://example.com/article"></link-metadata-preview>

The component renders inside its own shadow root, so its internal markup does not collide with your page CSS. Presentation is customized through attributes and CSS variables.

Runtime behavior

On connection, the component follows this order:

  1. If data-meta contains valid JSON, render that metadata immediately and skip fetching.
  2. Otherwise, if data-url exists, render a plain fallback link first.
  3. Fetch metadata from the API and replace the fallback with a full preview card.

If data-meta is present but invalid, the component logs a console error and falls back to data-url when one is available.

Request lifecycle

The default API request is:

https://api.linkmetadata.com/v1/metadata?url={encoded data-url}

The API is free to use. Requests made by the component count toward the public per-IP limit: 20 requests per 10 seconds, followed by a 10-second block when the limit is reached.

Multiple component instances with the same request URL share one in-flight fetch. After that request settles, the shared entry is removed; long-lived caching is handled by the browser and the LinkMetadata API.

When an instance is removed from the DOM, changes data-url, or switches to valid data-meta, it releases its active fetch. If no other instance is waiting for that same request, the component aborts the fetch. Stale responses are ignored, so a slow previous URL will not overwrite a newer card.

Fallbacks and failures

The component avoids showing technical errors to end users. If the metadata request fails, it keeps a plain linked fallback. If the preview image fails to load, the image area is hidden. If the favicon fails to load, only the favicon is hidden.

Card titles use the first useful value from title, og["og:title"], or twitter["twitter:title"]. If no title exists, the component promotes a short description to the title. If neither title nor description exists, it displays the hostname.

Images that are not marked cors_safe: true are loaded through LinkMetadata’s image proxy. Favicons are proxied only when favicon.cors_safe is explicitly false.

For details on cors_safe, supported proxy formats, and proxy errors, see Images and CORS.

Attributes

AttributeType / ValuesDefaultWhat it does
data-urlstring-The page you want to preview. Required unless data-meta is provided.
data-theme"light", "dark", "card""light"Picks one of the preset colour systems.
data-orientation"horizontal", "vertical""horizontal"Layout: image on the left vs. image on top. If omitted, narrow screens use vertical layout automatically.
data-show-image"true", "false""true"Show or hide the image.
data-show-favicon"true", "false""true"Show or hide the favicon next to the title.
data-show-branding"true", "false""true"Show or hide the “Preview by LinkMetadata.com” line.
data-max-widthCSS length"500px"Set the maximum card width, for example "720px" or "100%".
data-image-aspect-ratioCSS ratio or number-Override the image container aspect ratio, for example "16/9" or "1.91".
data-original-urlstring-Display this hostname instead of the canonical metadata URL.
data-api-endpointstringhttps://api.linkmetadata.com/v1/metadata?url=Prefix used before the encoded data-url.
data-metaJSON string-Render pre-fetched metadata and skip the component’s API request.

All attributes are reactive.

Boolean display attributes are string based. Use "false" to disable a feature; any other value behaves like enabled.

JavaScript properties

Every observed attribute also has a camel-cased property.

PropertyAttribute
dataUrldata-url
dataThemedata-theme
dataOrientationdata-orientation
dataShowImagedata-show-image
dataShowFavicondata-show-favicon
dataShowBrandingdata-show-branding
dataOriginalUrldata-original-url
dataApiEndpointdata-api-endpoint
dataMaxWidthdata-max-width
dataImageAspectRatiodata-image-aspect-ratio
dataMetadata-meta
const preview = document.querySelector("link-metadata-preview");

preview.dataUrl = "https://example.com/next";
preview.dataTheme = "card";
preview.dataShowImage = false;
preview.dataMeta = {
  title: "Cached title",
  description: "Rendered without a client-side API request.",
  url: "https://example.com/cached",
  image: { url: null },
  favicon: { url: null },
  og: {},
  twitter: {},
  safety_tags: []
};

Setting dataMeta to an object serializes it to JSON. Setting it to null removes data-meta; if data-url is present, the component fetches fresh metadata again.

Responsive layout

data-orientation="horizontal" puts the image beside the content. data-orientation="vertical" puts the image above the content. If data-orientation is omitted, the component uses horizontal layout on wider screens and switches to vertical below 480px.

Changing layout, theme, image visibility, favicon visibility, branding visibility, max width, or image ratio re-renders the existing metadata without making another API request.

Server-rendered metadata

Use data-meta when your backend, cache, or server-side render already has metadata. The component renders the card without making its own API request and still responds to later attribute changes.

For a complete example, see Render pre-fetched metadata.

CSS Variables

VariablePurposeDefault value
--lm-borderCard border shorthand1px solid #d7dce0
--lm-radiusCorner radius8px (12px in card theme)
--lm-bgCard background color#fff
--lm-shadowBase box-shadow0 1px 3px 0 rgba(0,0,0,.1), 0 1px 2px 0 rgba(0,0,0,.06)
--lm-hover-shadowShadow on hoverTheme-specific
--lm-transitionCard transitionbox-shadow 0.2s ease-in-out
--lm-max-widthMaximum card width500px
--lm-font-familyCard font stackSystem UI stack
--lm-content-paddingText block padding12px
--lm-content-padding-mobileText block padding under 480px12px
--lm-title-colorTitle text color#111
--lm-title-sizeTitle font size1em
--lm-title-weightTitle font weight600
--lm-title-line-heightTitle line height1.3
--lm-title-linesMaximum title lines2
--lm-desc-colorDescription text color#444
--lm-desc-sizeDescription font size0.875em
--lm-desc-line-heightDescription line height1.4
--lm-desc-linesMaximum description lines2
--lm-url-colorURL line color#555
--lm-url-sizeURL line font size0.75em
--lm-branding-colorBranding line color#777
--lm-branding-sizeBranding font size0.65em
--lm-branding-padding-topSpace above branding8px
--lm-fallback-bgMissing-image background#f0f0f0
--lm-fallback-icon-colorMissing-image icon color#aaa
--lm-image-widthThumbnail width in horizontal layout120px
--lm-image-aspect-ratioThumbnail aspect ratio1.5/1 horizontal, 16/9 vertical
--lm-image-fitThumbnail object-fitcover
--lm-image-positionThumbnail object-positioncenter
--lm-favicon-sizeFavicon width and height16px
--lm-favicon-gapGap between favicon and title8px

Stable Selectors

These .lm-* selectors are part of the styling contract.

SelectorWhat it is
.lm-cardThe anchor that forms the whole card
.lm-image-containerThe thumbnail wrapper
.lm-default-icon-containerWrapper shown when image is missing
.lm-icon-fallback svgChain-link SVG shown when image is missing
.lm-contentText column
.lm-title-wrapperFavicon and title row
.lm-faviconFavicon next to the title
.lm-titleTitle line
.lm-descDescription line
.lm-urlHostname line
.lm-brandingBranding tagline