Guide

SvelteKit Open Graph image

SvelteKit has no share-image setting. You write the tags yourself inside <svelte:head>, usually once in the root layout from data each page returns. Two things break the card: an image URL that is not absolute (including the prerender placeholder origin), and pages rendered only in the browser.

Tags in the root layout

SvelteKit's SEO guide says every page should have its own <title> and description inside <svelte:head>, and suggests a common pattern: return SEO data from page load functions and render it as page.data in the root layout. The svelte:head reference notes that during server-side rendering, head content is exposed separately from the body, so it lands in the HTML.

<!-- src/routes/+layout.svelte -->
<script>
  import { page } from '$app/state';
  let { children } = $props();
  const site = 'https://www.example.com';
  const image = $derived(new URL(page.data.ogImage ?? '/og/default.png', site).href);
</script>

<svelte:head>
  <title>{page.data.title ?? 'Example'}</title>
  <meta name="description" content={page.data.description} />
  <meta property="og:title" content={page.data.title} />
  <meta property="og:description" content={page.data.description} />
  <meta property="og:image" content={image} />
  <meta name="twitter:card" content="summary_large_image" />
</svelte:head>

{@render children()}

Use property="og:image", not name="og:image". The Open Graph protocol defines property, and some apps ignore the name form.

Each page sets its own image

// src/routes/blog/[slug]/+page.js
export async function load({ params, fetch }) {
  const post = await (await fetch(`/api/posts/${params.slug}`)).json();
  return {
    title: post.title,
    description: post.summary,
    ogImage: post.image, // '/og/my-post.png' or a full URL
  };
}

Pages that return no ogImage fall back to the default in the layout. Keep the image 1200 × 630; Open Graph image size explains why.

Absolute URLs and the prerender origin

Apps need a full https:// image URL. Absolute og:image URL lists which ones drop a relative path. The layout above builds it from a fixed site value. If you build it from page.url.origin instead, check prerendered pages: the configuration reference says url.origin defaults to http://sveltekit-prerender during prerendering. Every prerendered og:image then points at a host that does not exist. Set the origin option in svelte.config.js to your public site (kit.origin in current releases; older releases call it kit.prerender.origin), or use a fixed value as above.

Keep server rendering on

The SEO guide says SvelteKit uses SSR by default and you should leave it on unless you have a good reason. The page options reference warns that export const ssr = false in the root +layout.js turns the whole app into an SPA. On those pages the server sends no share tags, and link preview bots do not run JavaScript. Prerendering (export const prerender = true) is fine: the tags are written into the HTML file at build time. JavaScript and SPA previews covers the general problem.

Read the served HTML

curl -s https://www.example.com/blog/my-post | grep -E 'og:image|twitter:'

npm run dev serves on localhost, which no app can fetch. Check a deployed URL, or run npm run build and npm run preview and read what comes back. Then paste the live URL into the form above.

Checklist

  1. One <svelte:head> block (usually the root layout) writes the share tags, so pages do not add a second og:image.
  2. og:image uses property and an absolute https:// URL.
  3. Prerendered pages do not show http://sveltekit-prerender anywhere in the tags.
  4. Shared pages are server-rendered or prerendered, not ssr = false.
  5. After a change, apps that cached the old card get a refresh: see Clear Open Graph cache.

Other frameworks: Nuxt, Docusaurus, Next.js, Astro, and Gatsby.

FAQ

Where do the tags go?

In <svelte:head>, usually once in src/routes/+layout.svelte, reading page.data that each page's load returns.

Why does og:image start with http://sveltekit-prerender?

It was built from url.origin during prerendering. Set the origin option in svelte.config.js, or build the URL from a fixed public value.

Does ssr = false break previews?

Yes. The server HTML has no share tags, and bots do not run JavaScript. Keep SSR or prerendering on for shared pages.

What does a CardScope check cost?

A free check shows previews for X, LinkedIn, Slack, Discord, iMessage, Bluesky, Threads, Mastodon, WhatsApp, Telegram, and Microsoft Teams, plus the fix checklist. Pro is $19 once via Stripe Checkout, not a subscription: unlimited checks, a 50-page site audit with a CSV, share links, and PNG export. No account. A sample report shows a full result. A free audit checks the first 3 pages of a site and lists the rest. We parse HTML in memory and discard it — we do not store the page.