Skip to content

SEO head

Title, description, canonical, robots, hreflang, Open Graph and the Twitter card, from one component in your layout's head.

Native .astro · no React · MIT · free, no account

Install

Terminal
npx shadcn@latest add https://www.themes.dev/astro/r/seo.json

The CLI needs a components.json (from npx shadcn init), which also writes the tokens these files use. No CLI? Copy the files below to the same paths.

Files

src/kit/components/Seo.astroAstro
---// The head tags a page needs to be found and shared: title, description, canonical, robots, Open Graph and the// Twitter card. Put it in your layout's <head>. Set `site` in astro.config.mjs so the URLs are absolute.// <Seo title="Pricing" description="…" image={{ src: '/og/pricing.png', alt: '…', width: 1200, height: 630 }} />interface Props {  /** The page title, complete: add your site name yourself if you want it ("Pricing · Acme"). */  title: string;  /** One or two sentences, about 150 characters. */  description: string;  /** The canonical address; by default this page's own URL without query string. */  canonical?: string | URL;  /** The share image: absolute or root-relative, 1200×630 is the common size. */  image?: { src: string; alt: string; width: number; height: number };  /** 'article' for posts, with their dates. */  type?: 'website' | 'article';  published?: Date;  modified?: Date;  /** Keep the page out of search results (a thank-you page, a 404). */  noindex?: boolean;  /** Other language versions: [{ lang: 'nl', href: '/nl/prijzen/' }]. */  alternates?: { lang: string; href: string }[];  siteName?: string;}const { title, description, image, type = 'website', published, modified, noindex = false, alternates = [], siteName } = Astro.props;const base = Astro.site ?? Astro.url;const canonical = new URL(Astro.props.canonical ?? Astro.url.pathname, base).href;const abs = (s: string) => new URL(s, base).href;--- <title>{title}</title><meta name="description" content={description} /><link rel="canonical" href={canonical} />{noindex && <meta name="robots" content="noindex" />}{alternates.map((a) => <link rel="alternate" hreflang={a.lang} href={abs(a.href)} />)}<meta property="og:type" content={type} /><meta property="og:title" content={title} /><meta property="og:description" content={description} /><meta property="og:url" content={canonical} />{siteName && <meta property="og:site_name" content={siteName} />}{  image && (    <>      <meta property="og:image" content={abs(image.src)} />      <meta property="og:image:alt" content={image.alt} />      <meta property="og:image:width" content={String(image.width)} />      <meta property="og:image:height" content={String(image.height)} />    </>  )}{type === 'article' && published && <meta property="article:published_time" content={published.toISOString()} />}{type === 'article' && modified && <meta property="article:modified_time" content={modified.toISOString()} />}<meta name="twitter:card" content={image ? 'summary_large_image' : 'summary'} />