# SEO contracts

> Hold every page of a family to one page type, check each built page against it, and give an agent the contract before it writes the next page.

A site that publishes many pages of one kind, such as technique posts,
glossary entries, or alternatives pages, repeats the same structure on each of
them. An SEO contract writes that structure down once, as a page type in
`contentbit.seo.config.ts`. `contentbit check` holds every page of the type to
it, and `contentbit context` prints it for an agent before the agent writes or
edits a page.

The examples on this page come from a small Astro blog: one hub post,
`bread-basics`, and three technique posts that it links to.

## Connect the SEO config

Point `contentbit.config.ts` at the SEO config:

```ts
import type { ContentbitConfigInput } from 'contentbit'

export default {
  content: 'content/**/*.{md,mdx}',
  seo: './contentbit.seo.config.ts',
  links: {
    routes: { 'content/blog': '/blog/{path}' },
  },
} satisfies ContentbitConfigInput
```

`contentbit init --seo` writes both files. With
[page routes](/docs/reference/configuration#page-routes), `check` counts the
links that each page's built page renders.

## Define a page type

Each entry in `pageTypes` is a contract. This config has two types, and gives
the technique type to every file under `content/blog/`:

```ts
import type { SeoConfigInput } from 'contentbit'

export default {
  pageTypes: {
    guide: {
      requiredFrontmatter: ['title', 'description', 'keywords.primary'],
    },
    technique: {
      requiredFrontmatter: ['title', 'description', 'keywords.primary'],
      requiredLinksTo: ['bread-basics'],
      minOutgoingLinks: 2,
      minIncomingLinks: 1,
    },
  },
  pageDefaults: [{ pathPrefix: 'content/blog/', type: 'technique', intent: 'how-to' }],
} satisfies SeoConfigInput
```

A page type can hold these fields:

| Field | Meaning |
| --- | --- |
| `requiredFrontmatter` | Dot paths that must have a value after frontmatter normalization. An empty string or `null` counts as missing. |
| `requiredSections` | Headings that must appear in the document outline. Each entry has an `id` and the `headings` it accepts, such as `['Steps', 'How to do it']`. |
| `requiredBlocks` | Block names that the page must use at least once. |
| `recommendedBlocks` | Block names that give an `info` suggestion when the page does not use them. |
| `requiredLinksTo` | Pages that the page's content must link to, each named by key, then by slug, in the page's own locale. |
| `minOutgoingLinks` | The fewest other built pages that the page's content must link to. |
| `minIncomingLinks` | The fewest other built pages whose content must link to the page. |

Frontmatter is normalized before the checks run. A project that uses
`seoKeywords` also gets it as `keywords`, so a contract can require
`seoKeywords.primary` or `keywords.primary`.

## Give each page its type

A page takes its type from the first of these that has one:

1. A `type` field in its frontmatter.
2. Its entry in `pages`, described in [Plan a page before it exists](#plan-a-page-before-it-exists).
3. The first `pageDefaults` entry whose `pathPrefix` holds the file.

The hub post of the blog sets its own type and the key that `requiredLinksTo`
names:

```yaml
---
key: bread-basics
type: guide
title: Bread basics
description: The three steps of a loaf at home, from proofing the dough to baking it in a Dutch oven.
keywords:
  primary: how to make bread at home
---
```

Defaults fill in missing facts only. Frontmatter and `pages` entries win.

A type reaches a page through its Markdown file, so a built page with no
Markdown file, such as a page that a CMS renders, has no type and no
contract.

## Check the pages

Build the site, then run `check`. On the blog, one technique post breaks its
contract:

```text
contentbit check

  Build     dist, 6 pages
  Origin    https://bakery.example (inferred from canonicals)
  Markdown  4 files, 4 with a built page

Built-page findings
  warning CB_PAGE_LINK_REQUIRED  1 finding on 1 page
     hint Link to /blog/bread-basics/ from this page's content. Links in the header, nav, aside or footer, or outside the page content, do not count.
     /blog/baking/  Page type "technique" requires a link to "bread-basics" (/blog/bread-basics/), and this page's content has none.
  warning CB_PAGE_OUTGOING_LINKS_MIN  1 finding on 1 page
     hint Link to related pages from this page's content. Links in the header, nav, aside or footer, or outside the page content, do not count.
     /blog/baking/  This page's content links to 0 other pages, and page type "technique" asks for at least 2.

Markdown findings
  1. warning seo CB_SEO_FRONTMATTER_MISSING
     at content/blog/baking.md:1:1
     required frontmatter "keywords.primary" is missing
     hint Add keywords.primary to the frontmatter, with a value that is not empty.

  Errors    0
  Warnings  3
```

The link contract counts content links between built pages. A related-posts
list in an aside does not satisfy `minOutgoingLinks`, and neither does a
frontmatter `linksTo` that the page does not render. Only a built page is
held to the link contract.

Contract findings are warnings, so `check` still exits 0. Run
`contentbit check --strict-seo` to fail a CI job on them.

## Give the contract to an agent

`contentbit context <page>` ends with the contract of the page's type, as
acceptance checks:

```bash
contentbit context /blog/baking/
```

```md
## Contract

Page type: technique. Acceptance checks:

- Frontmatter includes title.
- Frontmatter includes description.
- Frontmatter includes keywords.primary.
- Document links to bread-basics.
- Document links to at least 2 other pages.
- At least 1 other page links to the document.

Related pages: Proofing dough, Scoring a loaf (planned), Shaping a loaf.
```

Above the contract, the page context lists the findings that break it, so an
agent sees both what the page must do and what it does not do yet.

## Plan a page before it exists

`pages` describes a page before its Markdown file exists:

```ts
pages: {
  scoring: {
    type: 'technique',
    key: 'scoring',
    slug: 'scoring',
    title: 'Scoring a loaf',
    keywords: { primary: 'how to score bread' },
  },
},
```

`contentbit context` takes the key or slug of a planned page, and prints what
the agent should write:

```md
# Planned page: scoring

The SEO config plans this page, and no Markdown file has its key yet. Write it to this contract, then build the site and run contentbit check.

## Declared

- Title: Scoring a loaf
- Description: none
- Type: technique
- Intent: none
- Primary keyword: how to score bread

## Contract

Page type: technique. Acceptance checks:

- Frontmatter includes title.
- Frontmatter includes description.
- Frontmatter includes keywords.primary.
- Document links to bread-basics.
- Document links to at least 2 other pages.
- At least 1 other page links to the document.
- Create the Markdown source file for this planned page.

Related pages: Baking in a Dutch oven, Proofing dough, Shaping a loaf.
```

The name of a `pages` entry is the page's key when the entry sets none, or a
file path such as `'content/blog/scoring.md'`. A file takes over a plan when
its key, its slug, or its path matches it, so every localized file that shares
the key of the plan counts as the created page. The plan and the frontmatter
then merge, and the frontmatter wins.

Each page has one SEO id: its key, or its slug when it has no key, with the
locale added for a localized page. When two pages have the same id, both stay
in the result, and the second gets a `CB_SEO_PAGE_ID_DUPLICATE` warning.

## Declare links in frontmatter

Pages can also declare their links in frontmatter. `linksTo` names other pages
by slug or key, and `aliases` keeps an old slug pointing at a page after a
rename:

```yaml
---
slug: cold-fermentation-pizza
aliases:
  - overnight-pizza-dough
linksTo:
  - pizza-flour-guide
keywords:
  primary: cold fermentation pizza dough
---
```

When one file or more declares a `slug`, `validate` checks every `linksTo`
target against the slugs, keys, and aliases of the other files. It reports a
target that matches no page, two pages with the same slug, an alias
that two pages claim, and a page that no other page names in `linksTo`. A link
in the page body does not count for these checks, so a site that declares
slugs and no `linksTo` gets a `CB_LINK_ORPHAN` warning on every page. Validate
the full content glob, so the check sees every page:

```bash
contentbit validate "content/**/*.md"
```

## Keep translated pages apart

For a site in several languages, give the pages of one topic the same `key`
and a slug in their own language:

```yaml
---
key: espresso-dial-in
locale: fr
slug: regler-espresso
linksTo:
  - espresso-grinder-settings
---
```

Then set `links.resolve` to `same-locale-key`, with the
[link resolver fields](/docs/reference/configuration#link-resolver-fields) that
name the locale, key, and slug:

```ts
links: {
  resolve: 'same-locale-key',
  localeField: 'locale',
  keyField: 'key',
  slugField: 'slug',
},
```

Contentbit keeps each localized page separate and resolves a link within the
page's own locale. A `linksTo` target that exists only in another locale is an
error, `CB_LINK_LOCALE_MISSING`, and a required link names a page in the
page's own locale too.
