Built-in SEO Features
EmDash provides search engine optimization (SEO) features for content entries, page heads, sitemaps, robots.txt, and redirects. Most of them work once a collection has SEO enabled and the site renders <EmDashHead> in its page heads.
| Feature | Output | Requires |
|---|---|---|
| SEO panel | Title, description, image, canonical URL, and no-index per entry | SEO enabled on the collection |
| Head metadata | Description, robots, canonical, Open Graph, and Twitter Card tags | <EmDashHead> in the page head |
| Structured data | JSON-LD BlogPosting or WebSite |
<EmDashHead> in the page head |
| Sitemaps | /sitemap.xml and /sitemap-{collection}.xml |
SEO enabled on the collection |
robots.txt |
/robots.txt with a sitemap reference |
Nothing |
| Staging | noindex on every page |
A site created with the setup wizard; <EmDashHead> for the tag |
| Site-wide settings | Verification tags, default social image, title separator | Values in Settings > SEO |
| Translations | hreflang links in the head and the sitemap |
Astro i18n configured |
| Redirects and 404 log | Redirect and gone rules, a log of missed URLs | Nothing |
Enable SEO on a collection
Section titled “Enable SEO on a collection”Turn on SEO for a collection in Content Types in the EmDash admin. In a seed file, add "seo" to the collection’s supports array:
{ "version": "1", "collections": [ { "slug": "posts", "label": "Posts", "urlPattern": "/posts/{slug}", "supports": ["drafts", "revisions", "seo"], "fields": [{ "slug": "title", "label": "Title", "type": "string", "required": true }] } ]}With SEO enabled, the editor of a saved entry shows an SEO panel, and the collection appears in the sitemap. Collections with Routable turned off stay out of the sitemap.
Edit SEO fields per entry
Section titled “Edit SEO fields per entry”The SEO panel in the entry editor holds these fields:
- OG Image is the social preview image (
og:image) and the image listed for the entry in the sitemap. - SEO Title replaces the entry title in social previews and structured data, and in
<title>when the page renders the title fromgetSeoMeta(). - Meta Description is the summary shown below the title in search results. The panel counts characters against a 160-character guideline.
- Canonical URL points search engines to the original version of a page that is duplicated from another URL.
- Hide from search engines adds
noindex, nofollowto the page and removes the entry from the sitemap and fromhreflangalternates.
Empty fields fall back to the values the page passes to createPublicPageContext(). getSeoMeta() falls back to the entry’s title and its excerpt field. For an entry from getEmDashEntry() or resolveEmDashPath(), the title comes from the field named by the collection’s titleField, or from the title field when the collection sets no titleField or the entry leaves that field empty. For entries from other queries, the title comes from the title field.
Imported or programmatically written SEO data can store a bare media ID. The editor can show that image while shared links still point to a missing file. When Sharing image needs a URL appears below the loaded preview, choose Save image URL to replace the ID with the file URL used for social previews and sitemap images. Viewing the preview alone does not change the saved value. If you see Image not found, check that the media is available and try reloading the editor before replacing the image.
Render SEO metadata on a content page
Section titled “Render SEO metadata on a content page”<EmDashHead> renders the SEO metadata for a page context. On a content page, pass the entry’s collection and database ID as content, so <EmDashHead> applies the entry’s SEO panel values.
The following example renders the head of a post page. getSeoMeta() resolves the <title> and canonical URL, and <EmDashHead> renders the remaining tags:
---import { decodeSlug, getEmDashEntry, getSeoMeta, getSiteSettings } from "emdash";import { createPublicPageContext } from "emdash/page";import { EmDashHead } from "emdash/ui";
const slug = decodeSlug(Astro.params.slug);if (!slug) return Astro.rewrite("/404");
const { entry: post } = await getEmDashEntry("posts", slug);if (!post) return Astro.rewrite("/404");
const settings = await getSiteSettings();const seo = getSeoMeta(post, { siteTitle: settings.title, siteUrl: settings.url || Astro.url.origin, titleSeparator: settings.seo?.titleSeparator, path: Astro.url.pathname,});
const page = createPublicPageContext({ Astro, kind: "content", title: seo.title, pageTitle: seo.ogTitle, description: seo.description, canonical: seo.canonical, siteName: settings.title, content: { collection: "posts", id: post.data.id, slug: post.data.slug },});---
<html lang="en"> <head> <meta charset="utf-8" /> <title>{seo.title}</title> <EmDashHead page={page} /> </head> <body> <h1>{post.data.title}</h1> </body></html>