Link preview

A link shown as a card: the page's title, description, image and site, as a chat, a comment or a composer shows a pasted URL.

Link preview

No JavaScript needed. Only dismiss needs Alpine and the package's script; without them the preview shows with no remove button. See getting started.

Use it

The component draws what your server already found. It fetches nothing.

<x-ds::link-preview
    :url="$link->url"
    :title="$link->title"
    :description="$link->description"
    :image="route('og-image', $link)"
    :site="$link->site_name"
    :favicon="$link->favicon_url"
    :publishedAt="$link->published_at"
/>

Props

PropTypeDefaultWhat it does
urlstring—The page. Only an http or https URL becomes a link; anything else (javascript:, data:) is shown as text.
titlestring—The page's title, held to two lines. Without one the domain stands in.
descriptionstring—Held to two lines (not shown in compact).
imagestring—The page's image. A failed image leaves a placeholder, not a hole.
sitestring—The site's name, before the domain.
faviconstring—A small icon before the site.
authorstring—Shown with the date.
publishedAtstring or DateTime—Shown as "3 Dec 2026".
variantrow card compactrowImage at the side, image on top, or one line.
loadingboolfalseYou are still fetching: a skeleton in its place.
failedboolfalseFetching failed: a plain link showing the domain.
targetstring_blankWhere it opens; _self (or an empty string) for the same tab. A new tab is said in the link's name.
dismissboolfalseA remove button, for a preview in a composer: hides it and dispatches dismiss.
dismissLabelstringRemove previewThat button's name.

Fetching the metadata, safely

Reading a stranger's page from your server is a request your server makes on someone else's say-so, which is how internal services get reached (server-side request forgery). When you fetch Open Graph data:

  • Resolve the host and refuse private addresses: loopback, link-local (including the cloud metadata address 169.254.169.254), and the private ranges, for IPv4 and IPv6. Check again after every redirect, and allow only a few.
  • Only http and https, on ports 80 and 443.
  • Bound it: a short timeout, and stop reading after a few hundred kilobytes; the <head> is near the top.
  • Treat everything you read as text. Blade escapes it here; do not mark it safe.
  • Proxy the image through your own host (and cache it) rather than hot-linking it: the reader's browser then never tells the other site who is reading, and a changed image cannot change what was posted.
  • Cache by URL, so a link pasted a thousand times is fetched once.

Render loading while the job runs and failed if it gives up, so the URL is never lost.

In a composer

<x-ds::link-preview :url="$draft->link" :title="$draft->link_title" :dismiss="true"
    x-on:dismiss="$wire.removePreview()" />

Accessibility

  • The whole card is one link, named by its title, domain and "opens in a new tab", and described by its description, rather than read out end to end.
  • Images are decorative (alt=""): the title says what the link is.
  • The remove button is a separate button after the link, not inside it.

Don't

  • Don't pass the URL through {!! !!} or build the link yourself; the component's checks are the point.
  • Don't fetch metadata in the request that renders the page. Fetch in a job and show loading.
  • Don't hot-link the image where you can proxy it.

More in specs/link-preview.md.


From the package repo: edit this page on GitHub.

Was this page helpful?