Skip to Content
Vuesho

SEO

Head tags, sitemaps and Open Graph images.

Every page has the head tags that search engines and social cards read. The recommended preset also serves robots.txt, plus a sitemap and an RSS feed once you set the site's URL.

Site URL

site.baseUrl is your deployed site's origin, like https://docs.acme.dev, without the base path. A site at https://acme.github.io/docs/ sets https://acme.github.io, and the build adds the base path to URLs.

vuesho.config.ts
import { createSite, recommendedPreset } from "vuesho/config";
import { defineDocs } from "vuesho/content";

const docs = defineDocs();

export default createSite(docs.toSource())
  .plugins(recommendedPreset())
  .config({ site: { baseUrl: "https://docs.acme.dev" } }); 

The canonical link, the sitemap, the RSS feed and the Open Graph images need full URLs, so a site without site.baseUrl has none of them, and the build warns.

Head Tags

Each page has these tags in its <head>, from its title, its description and the site name:

<html lang="en" dir="ltr">
  <title>Installation | Acme Docs</title>
  <meta name="description" content="Add Acme to your project." />
  <meta property="og:title" content="Installation" />
  <meta property="og:description" content="Add Acme to your project." />
  <meta property="og:site_name" content="Acme Docs" />
  <link rel="alternate" type="text/markdown" href="/guide/install.md" />
  <!-- With `site.baseUrl`. -->
  <link rel="canonical" href="https://docs.acme.dev/guide/install" />
  <meta property="og:url" content="https://docs.acme.dev/guide/install" />
  <!-- With an Open Graph image. -->
  <meta property="og:image" content="https://docs.acme.dev/guide/install.webp" />
  <meta name="twitter:card" content="summary_large_image" />
  <!-- With translations, on a site with several languages. -->
  <link rel="alternate" hreflang="en" href="https://docs.acme.dev/guide/install" />
  <link rel="alternate" hreflang="zh-CN" href="https://docs.acme.dev/cn/guide/install" />
  <link rel="alternate" hreflang="x-default" href="https://docs.acme.dev/guide/install" />
  <!-- On a page left out of search engines. -->
  <meta name="robots" content="noindex" />

head in .config() adds tags to every page. An html or body tag sets that element's attributes, and Vuesho adds the base path to paths on your site:

vuesho.config.ts
createSite(docs.toSource()).config({
  head: [
    { tag: "link", attrs: { rel: "icon", href: "/favicon.svg" } },
    { tag: "meta", attrs: { name: "theme-color", content: "#0a0a0a" } },
    { tag: "html", attrs: { class: "acme" } },
  ],
});

As a function, head returns each page's tags, from its path, lang and pageData:

vuesho.config.ts
createSite(docs.toSource()).config({
  head({ path }) {
    // A post of the blog is an article for social cards.
    if (!path.startsWith("/blog/")) return [];
    return [{ tag: "meta", attrs: { property: "og:type", content: "article" } }];
  },
});

Your tag replaces the one with the same key:

  • the <title>
  • a <meta> by its name, property or http-equiv
  • the canonical link
  • an hreflang link

Tags in the config take priority over the defaults, plugins and the theme. A tag without a key is added.

Sitemap

/sitemap.xml lists every page, with its translations and last modified date, except pages with noindex. getEntry changes a page's entry, and additionalEntries adds URLs, like a custom page rendered on request:

vuesho.config.ts
createSite(docs.toSource()).plugins(
  recommendedPreset({
    sitemap: {
      getEntry(entry, { page }) {
        if (!page?.url.startsWith("/blog/")) return entry;
        return { ...entry, changefreq: "weekly", images: [{ loc: "/blog/cover.png" }] };
      },
      additionalEntries: [{ loc: "/releases/v2", priority: 0.5 }],
    },
  }),
);

An entry takes the fields of the sitemap protocol, and Google's images, videos and news. Write paths without the base path, which the sitemap adds.

Prop

Type

For a sitemap that your own plugin serves, buildSitemap() from vuesho/config writes the XML from entries.

robots.txt

/robots.txt allows every crawler, and links to the sitemap. rules replaces its rules:

vuesho.config.ts
createSite(docs.toSource()).plugins(
  recommendedPreset({
    robots: {
      rules: [
        { userAgent: "*", allow: "/", disallow: ["/private", "/*.pdf$"] },
        { userAgent: "GPTBot", disallow: "/" },
      ],
    },
  }),
);

Prop

Type

A rule takes these fields:

Prop

Type

Sites Under a Base Path

Crawlers read robots.txt only at a domain's root. A site at /docs/ serves /docs/robots.txt, so copy its rules to the robots.txt at the domain's root.

RSS Feed

/rss.xml lists the 20 newest pages with a date, from the frontmatter's date or from Git. Each page links to it in its <head>, and each language has its own feed, like /cn/rss.xml:

vuesho.config.ts
createSite(docs.toSource()).plugins(
  recommendedPreset({
    rss: {
      filter: ({ page }) => page.url.startsWith("/blog/"),
      channel: { title: "Acme Blog", image: "/logo.png" },
      getItem: (item) => ({ ...item, author: "team@acme.dev (Acme)" }),
      limit: 50,
    },
  }),
);

Prop

Type

rss: false turns off the feed. For another feed that your own plugin serves, buildRSS() from vuesho/config writes the XML.

To serve the feed without the link in <head>, add rssPlugin() without its head hook:

vuesho.config.ts
import { rssPlugin } from "vuesho/config"; 

createSite(docs.toSource()).plugins(
  recommendedPreset({ rss: false }), 
  { ...rssPlugin({ limit: 50 }), head: undefined }, 
);

Excluded Pages

There are two ways to leave a page out:

  • The filter of the sitemap, the RSS feed, llms.txt or the search leaves a page out of that output only, and a crawler can still follow a link to the page.
  • noindex in the frontmatter, or from a plugin's transformPage, adds <meta name="robots" content="noindex"> and leaves the page out of every output.

Filtering by Frontmatter

A custom frontmatter field, like unlisted, can leave a page out of each file through filter. Add it to the frontmatter schema:

vuesho.config.ts
import { pageSchema } from "vuesho/schema"; 
import { z } from "zod"; 

const docs = defineDocs({
  docs: { schema: pageSchema.extend({ unlisted: z.boolean().default(false) }) }, 
});

export default createSite(docs.toSource()).plugins(
  recommendedPreset({
    sitemap: { filter: ({ page }) => !page?.data.unlisted },
    rss: { filter: ({ page }) => !page.data.unlisted },
    llms: { filter: ({ page }) => !page.data.unlisted },
    search: { filter: ({ page }) => !page.data.unlisted },
  }),
);

The sitemap's filter receives { page, route, path, lang }, so it can also leave out custom pages by path. A page left out of llms loses its .md file too.

Setting noindex

To set noindex on every page under a path, use a plugin's transformPage hook:

vuesho.config.ts
createSite(docs.toSource()).plugins(
  recommendedPreset(),
  {
    name: "site:noindex",
    transformPage: (_data, { path }) =>
      path.startsWith("/internal/") ? { noindex: true } : undefined,
  },
);

A custom page sets it with definePage({ data: { noindex: true } }). A page shown in the fallback language already has noindex.

Open Graph Images

ogImagePlugin() draws a 1200 by 630 pixel image for each page, with its title, its description and the site name, for social cards. Here is this page's image:

This page's Open Graph image, with its title and description

npm create vuesho adds the plugin to a new site. To add it to an existing site, follow these steps.

Install Takumi

Takumi renders the images:

npm i takumi-js

Add the Plugin

The image tags need full URLs, so set the site URL:

vuesho.config.ts
import { ogImagePlugin } from "vuesho/og"; 

export default createSite(docs.toSource())
  .plugins(
    recommendedPreset(),
    ogImagePlugin(), 
  )
  .config({ site: { name: "Acme Docs", baseUrl: "https://docs.acme.dev" } }); 

Prop

Type

Open an Image

Run vuesho dev, and open a page's URL with .webp added, like /guide/install.webp, or /index.webp for /.

Pages With an Image

Every page has an image, including custom pages and plugin pages, unless it has noindex or a filter leaves it out:

vuesho.config.ts
ogImagePlugin({ filter: ({ path }) => !path.startsWith("/internal/") });

To use an image of your own on a page, leave the page out with filter, and write its tags with head. A social card reads a full URL only:

vuesho.config.ts
createSite(docs.toSource())
  .plugins(recommendedPreset(), ogImagePlugin({ filter: ({ path }) => path !== "/" }))
  .config({
    head({ path }) {
      if (path !== "/") return [];
      return [
        {
          tag: "meta",
          attrs: { property: "og:image", content: "https://docs.acme.dev/banner.png" },
        },
        { tag: "meta", attrs: { name: "twitter:card", content: "summary_large_image" } },
      ];
    },
  });

Texts

The image shows the page's title and description. ogImage in the frontmatter replaces them:

content/reference/client.md
---
title: Client
ogImage:
  title: "API: Client"
  description: The client of the Acme API
---

To set the texts of many pages, use a plugin's transformPage, which declares ogImage in its types:

vuesho.config.ts
import type { AppShape, Plugin } from "vuesho/config";

const referenceImages: Plugin<AppShape, { pageData: { ogImage?: { title?: string } } }> = {
  name: "site:og",
  transformPage(data, { page }) {
    if (!page?.path.startsWith("reference/")) return;
    return { ogImage: { title: `API: ${data.title}` } };
  },
};

createSite(docs.toSource()).plugins(recommendedPreset(), ogImagePlugin(), referenceImages);

Template

The image is a Vue component, the theme's og-image body. To use your own template, replace it:

src/theme.ts
import { defineTheme } from "vuesho/theme";
import defaultTheme from "vuesho/theme/default";

export default defineTheme({
  extends: defaultTheme,
  bodies: { "og-image": () => import("./OGImage.vue") }, 
});

The template renders without your site's CSS, so style its elements with style:

src/OGImage.vue
<script setup lang="ts">
import type { OGImageProps } from "vuesho/og";

// The texts of `ogImage`, or the page's title and description.
const { title, description, site } = defineProps<OGImageProps>();
</script>

<template>
  <div
    :lang="site.lang"
    :dir="site.dir"
    style="
      display: flex;
      flex-direction: column;
      width: 100%;
      height: 100%;
      padding: 64px;
      color: white;
      background: #0a0a0a;
    "
  >
    <p style="margin: 0; font-size: 72px; font-weight: 700">
      {{ title }}
    </p>
    <p style="font-size: 36px; color: #a3a3a3">
      {{ description }}
    </p>
    <p style="margin-top: auto; font-size: 36px">{{ site.name }}</p>
  </div>
</template>

Prop

Type

Takumi loads an image from a full URL or a data URL, not from a path like /logo.png. Import a project file as a data URL with ?inline:

src/OGImage.vue
<script setup lang="ts">
import logo from "./logo.png?inline";
</script>

<template>
  <img :src="logo" width="64" height="64" />
</template>

Fonts

The images draw Latin text with Takumi's Geist, and other scripts with their Noto font from Google Fonts. To use other fonts, or for an offline build, give fonts in options:

vuesho.config.ts
import { googleFonts } from "takumi-js/helpers";
import { ogImagePlugin } from "vuesho/og";

ogImagePlugin({
  // Downloads only the characters the pages use.
  options: ({ lang }) => ({
    fonts: googleFonts(lang === "ja" ? ["Inter", "Noto Sans JP"] : ["Inter"]),
  }),
});

A template sets a font from options with fontFamily, like Playfair Display for blog posts:

import { googleFonts } from "takumi-js/helpers";
import { ogImagePlugin } from "vuesho/og";

ogImagePlugin({
  options: () => ({ fonts: googleFonts(["Inter", "Playfair Display"]) }), 
});

Deployment

Build on the operating system and CPU architecture you deploy to, so the native build of Takumi matches the server.

Cloudflare Workers' CPU Limit

Cloudflare Workers' free plan limits a request to 10 ms of CPU time, which drawing an image can exceed. Keep the pages static there, so the build draws their images.