---
title: "Docs Engine"
description: "Serve a @farming-labs/docs-powered docs runtime from Farm.js config, including human pages and agent-readable API routes."
canonical_url: "https://mcp.farmjs.dev/docs/0.1/docs-engine"
markdown_url: "https://mcp.farmjs.dev/docs/0.1/docs-engine.md"
last_updated: "2026-12-01"
x_farming_labs_generated_preamble: true
---

# Docs Engine
URL: /docs/0.1/docs-engine
LLM index: /llms.txt
Description: Serve a @farming-labs/docs-powered docs runtime from Farm.js config, including human pages and agent-readable API routes.

# Docs Engine

Serve a @farming-labs/docs-powered docs runtime from Farm.js config, including human pages and agent-readable API routes.

Farm.js supports the built-in docs renderer and edge-capable external adapters on Node.js and edge
deployment targets. Edge production builds compile the docs tree into a runtime-neutral content
snapshot, so Workers serve HTML, client-navigation data, Markdown, search, sitemap, robots, and
agent-discovery routes without filesystem access.

## Enable docs

The docs runtime renders with `@farming-labs/docs`, which the app installs:

```bash
pnpm add @farming-labs/docs
```

When docs are enabled (in `farm.config.ts`, or by a discovered `docs.config.*` or `docs.json`) and
the package is missing, `farm dev` and `farm build` stop with that install command. The React
starters already declare it.

**farm.config.ts**

```ts
import { defineConfig } from "@farm.js/core";

export default defineConfig({
  docs: {
    entry: "/docs",
    metadata: {
      description: "Product guides and API reference.",
    },
    nav: {
      title: "Acme Docs",
    },
  },
});
```

Farm.js uses `src/app/docs` for content when `entry` is `/docs`. Add markdown pages using the normal
app directory structure:

```txt
src/app/docs/
  page.md
  getting-started/
    page.md
  api-reference/
    page.md
```

These files are the docs engine's pages, which it answers before the app's page routes in
development and production. An app page under the docs entry renders only at a path without a docs
page.

The built-in renderer draws the app's layouts with client components around docs pages in
development and production, whether a layout hydrates whole or its client components hydrate as
islands of their own, so those components (a theme toggle, a sidebar provider, a counter) work there
too. A link between an app page and a docs page loads the other page as a document.

Use frontmatter for page metadata:

```md
---
title: "Getting Started"
description: "Install Farm.js and create your first application."
---

# Getting Started
```

Every Markdown docs page also gets a page-specific 1200 by 630 social preview automatically. Farm.js
uses the title, description, section, and route to emit Open Graph and X metadata, then selects a
technical illustration suited to the page topic. The image is content-fingerprinted and served
with immutable caching.

X, LinkedIn, Facebook, and Slack do not render SVG previews, so when the optional `sharp` package
is installed (`pnpm add sharp`), `og:image` points at a PNG
(`/docs/_social/<page>/opengraph-image.png`) rendered from the same design. Without `sharp`, the
SVG is the preview. The SVG stays available at `opengraph-image.svg` either way. The PNG uses system
fonts, because `socialImage.fonts` can only be embedded in the SVG. Edge targets render the PNGs at
build time into the server bundle, which adds roughly 25-35 KB per page.

Use frontmatter only when a page needs to override the automatic result:

```md
---
title: "Authentication"
description: "Protect routes and verify sessions."
socialTitle: "Authentication in Farm.js"
socialDescription: "Typed, server-side authentication for full-stack React apps."
socialIllustration: "auth"
---
```

`socialIllustration` accepts `auth`, `cache`, `cli`, `integrations`, `project`, `routing`, or
`runtime`. Set `socialImage` to an absolute or root-relative image URL to supply a custom preview,
or set it to `false` to disable the preview for one page.

## Automatic docs routes

When `docs.entry` is enabled, Farm.js serves the docs entry and `/api/docs` machine endpoints
automatically. Route wrappers are only needed when you want to override the default behavior.

- /docs
- /docs/getting-started
- /docs/getting-started.md
- /api/docs?format=llms
- /api/docs?format=sitemap-xml
- /api/docs/agent/spec

### Base path

With an [application `basePath`](/docs/0.1/configuration#application-base-path) such as `/console`, the
docs answer below it: `/console/docs`, `/console/docs/getting-started.md`, `/console/llms.txt`,
`/console/sitemap.xml`, `/console/robots.txt`, and the other docs files. Every link the docs runtime
generates includes the base path: the navigation, breadcrumbs, canonical and social image URLs, the
URLs in a page's Markdown, `llms.txt`, `llms-full.txt`, the sitemaps, and search results. As app
pages do, the docs also keep answering at the origin root.

The `/api/docs` endpoints stay at `/api/docs`, as the app's API routes do. Links you write in a
docs page are rendered as written. The routes `@farming-labs/docs` lists in `AGENTS.md`, `SKILL.md`,
the agent manifest, and a page's `LLM index` line are root-relative, and answer at the origin root.
An external docs adapter such as `@farming-labs/farmjs` routes and links its pages itself, and does
not apply the base path.

`/api/docs?format=config` is public, so it returns only the documented docs config fields with
credentials replaced by `"[redacted]"`: values under `apiKey`, `token`, `secret`, `password`,
`authorization`, or `credential` keys, and every `headers` value. The server keeps the real values
for search, Ask AI, and Docs Cloud. `/api/docs?format=diagnostics` reports filesystem paths, so
only `farm dev` serves it; production answers 404.

### Private docs pages

Guard docs pages the way you guard app pages: a `middleware.ts` in the docs folder, a config
middleware `matcher` such as `["/docs/internal", "/docs/internal/:path*"]`, or an integration's
`protectedRoutes`. The guard decides who reads the page. The `/api/docs` endpoints, `llms.txt`,
`llms-full.txt`, and the sitemaps answer anyone, so they leave out every docs page a guard written
for its path covers: search skips it, its titles stay unlisted, and `/api/docs/<slug>.md` answers
not found. Guards that cover every path (a root `middleware.ts`, or middleware without a matcher,
with a catch-all matcher, or with a function matcher) name no page and hide nothing, the same rule
as the [sitemap](/docs/0.1/configuration#sitemap). A function matcher cannot be read at build time, so
give private docs a path matcher, or guard `/api/docs` and the llms files as well.

A page's `.md` URL is served only when its guard covers that URL too. `/docs/internal/:path*` never
meets `/docs/internal.md`, so that file answers 404; name it in the matcher to serve it to signed-in
readers. A docs response to a request with cookies or an `Authorization` header, or one middleware
attached request data to, is sent as `Cache-Control: private, no-store`, as app pages are, so a CDN
never hands a signed-in reader's copy to someone else.

Only a page's own URL reaches it: `/docs/internal//secret`, `/docs/Internal/secret`, or
`/api/docs?format=markdown&path=internal/./secret` answer not found rather than serving the file
under a URL its guard never sees.

An external docs adapter serves pages, Markdown, `llms-full.txt`, search, Ask AI, and MCP from its
own index. Farm.js passes it the app's guards when it declares the `guarded-pages` capability, as
`@farming-labs/farmjs` does from the release that adds it: a page a guard covers stays out of
search, `llms.txt`, `llms-full.txt`, the sitemap, MCP, Ask AI, and the content feed, and its
Markdown answers only at a URL the guard also covers, as with the built-in runtime. On an edge
target the adapter serves a build-time snapshot that runs no guard, so `farm build` stops with an
error naming the pages there; deploy guarded docs to a Node or serverless target. An adapter
without the capability makes `farm dev` and `farm build` stop with that error on every target:
upgrade it, move private pages out of the docs folder, or set `docs.adapter: false` to use Farm.js's
built-in docs runtime.

## Docs adapters on any renderer

A docs adapter is a docs engine plus a UI. Its descriptor names the UI module `ui` and the renderer
it runs under `renderer`. Farm.js loads `ui` on the server through `loadUIModule()`, and in the browser
calls its `hydrateFarmDocs()` on the pages the adapter rendered. A descriptor without `renderer` has
a React UI, and one written before `ui` existed names it `react`, which Farm.js still reads.

Farm.js hands the UI the app's renderer. On the server, `renderFarmDocsPage()` receives `renderer`:
its `name`, `createElement()`, `Fragment`, `renderToString()`, and `generateHydrationScript()` when
the renderer has one, such as Solid. In the browser, `hydrateFarmDocs()` receives the renderer's
client module, with `createElement()` and `hydrateRoot()`. A UI built from those runs under any
renderer.

`@farming-labs/farmjs` ships one. On a renderer other than React, a custom one included,
`withDocs()` alone builds the docs pages with the app's renderer:

```ts
import { defineConfig } from "@farm.js/core";
import { withDocs } from "@farming-labs/farmjs/config";
import { leaf } from "farm-leaf";

export default withDocs(defineConfig({ renderer: leaf() }));
```

The pages keep the React UI's layout and stylesheet. Their Markdown compiles to HTML on the server,
so `react` stays a server dependency and no React reaches the browser. This UI has no search box
or client navigation between docs pages. Its table of contents shows on wide screens, with no
popover on narrow ones, and theme components such as tabs render as static HTML. It runs on the
Node.js runtime: edge builds stop because it has no edge compiler.

A renderer checks that it writes and hydrates these pages as the UI expects with the suite in
`@farming-labs/farmjs/testing`, run from a Vitest file with a DOM environment:

```ts
// @vitest-environment jsdom
import { defineFarmDocsRendererConformance } from "@farming-labs/farmjs/testing";
import * as client from "../src/client.js";
import * as server from "../src/server.js";

defineFarmDocsRendererConformance({ name: "leaf", server, client });
```

A renderer that writes attributes for its own hydration lists them in `hydrationAttributes`, such
as `["data-hk"]` for Solid, so the HTML comparison skips them.

A UI written for one renderer replaces it through `ui`:

```ts
export default withDocs(defineConfig({ renderer: leaf() }), {
  ui: { module: "@acme/leaf-docs", renderer: "leaf" },
});
```

The app's Vite pipeline compiles the UI and the docs config module with its renderer, so an adapter
runs only under the renderer its UI names. `farm dev` and `farm build` stop when they differ, and
name the fix. Farm.js's built-in renderer (`docs.adapter: false`) serves docs under any renderer.

## Configure the docs experience

Keep docs configuration alongside the rest of the application in `farm.config.ts`:

**farm.config.ts**

```ts
import { defineConfig } from "@farm.js/core";

export default defineConfig({
  docs: {
    entry: "/docs",
    nav: {
      title: "Acme Docs",
      url: "/",
    },
    search: {
      provider: "simple",
      enabled: true,
      maxResults: 10,
    },
    socialImage: {
      baseUrl: "https://docs.example.com",
      siteName: "Acme",
      brand: "Acme",
    },
    pageActions: {
      copyMarkdown: {
        enabled: true,
      },
    },
    llmsTxt: {
      enabled: true,
      siteTitle: "Acme Docs",
    },
    sitemap: true,
    robots: true,
  },
});
```

The `docs` object controls the public path, metadata, navigation, search, page actions, agent output,
theme, icons, reading time, last-updated display, sitemap, and robots output. `defineDocs` is not
required for a Farm.js application.

### Built-in search

Farm.js uses the existing `DocsCommandSearch` React component from `@farming-labs/theme`, the same Omni
search used by the Next.js docs adapter. Farm.js provides its HTML mount and server endpoint; the shared
component owns the dialog, fuzzy ranking, filters, recent searches, loading and empty states, result
highlights, and keyboard navigation. The sidebar control and `Cmd+K` / `Ctrl+K` shortcut open it.

The component queries `/api/docs?query=<term>`. That endpoint returns the standard docs search result
array, so `provider`, `maxResults`, and custom provider behavior come from the same `docs.search`
config instead of a second client-side search implementation.

Use `search: true` for the built-in simple provider, or configure `provider: "simple"`,
`"algolia"`, `"typesense"`, `"mcp"`, or `"custom"`. Search result content is rendered as text, and
provider credentials remain on the server. Setting `search: false` or `search.enabled: false`
disables both the endpoint capability and the interface.

### Optional external config

Large documentation sites may move the serializable docs options into `docs.config.ts`,
`docs.config.js`, `docs.config.mjs`, `docs.config.cjs`, or `docs.json`. Farm.js discovers those files
automatically. Values declared in `farm.config.ts` take priority, so applications can still override
the entry or any shared setting.

## Several docs roots

One app can serve several independent docs sets, each under its own route, with its own content,
navigation, and search. Declare the extra sets once, in `roots` in the docs config file; the docs
config itself stays the first, main root.

**docs.config.tsx**

```tsx
import { defineDocs } from "@farming-labs/docs";

export default defineDocs({
  entry: "docs",
  nav: { title: "Acme Docs" },
  roots: [
    {
      entry: "statewire/docs",
      nav: { title: <span>Statewire</span> },
      metadata: { titleTemplate: "%s – Statewire" },
    },
    { entry: "pinboard/docs", contentDir: "content/pinboard", nav: { title: "Pinboard" } },
  ],
});
```

**farm.config.ts**

```ts
import { defineConfig } from "@farm.js/core";

export default defineConfig({
  docs: {
    entry: "/docs",
    // Optional: serializable overrides for one root, matched by entry.
    roots: [{ entry: "/statewire/docs", search: { enabled: false } }],
  },
});
```

- Each root inherits the main root's settings and replaces the fields it sets itself.
- `docs.roots` in `farm.config.ts` takes the same flat shape. An entry there merges over the docs
  config's root with the same `entry`, its fields winning, or adds a root of its own. From lowest to
  highest precedence: the docs config's main fields, the `docs` options in `farm.config.ts`, the
  docs config's root, and that root's `docs.roots` entry.
- `contentDir` defaults to `src/app/<entry>`, as it does for the main root, and is never inherited.
- Icons, React elements, MDX components, and callbacks in a root's docs config reach the adapter as
  written; Farm.js serializes only the plain values.
- `/sitemap.xml`, `/robots.txt`, `/llms-full.txt`, docs MCP, and the AI catalog entry come from the
  first root.
- Search follows the page it is opened from: it searches that page's root. `/api/docs?root=<entry>`
  or `?pathname=<page>` names the root explicitly.

Several roots need a docs adapter such as `@farming-labs/farmjs` on a Node.js deployment target,
Vercel included. `farm dev` and `farm build` stop with an error for the built-in renderer
(`docs.adapter: false`) and for edge targets. Each root's `entry` must be unique and cannot be `/`.

## Agent-readable output

The docs runtime can serve human pages and machine-readable output from the same markdown source:

- `.md` mirrors for individual pages.
- `llms.txt` style summary output.
- Sitemap output.
- Agent discovery/spec routes.
- Search metadata.

That means docs content only needs to be written once.

An app's own root `sitemap.ts`, `robots.ts`, `llms.ts`, or `llms-full.ts`
[metadata route](/docs/0.1/routing#application-metadata-routes), or the same file in `public/`, takes
`/sitemap.xml`, `/robots.txt`, `/llms.txt`, or `/llms-full.txt` from the docs runtime, and so does a
robots.txt generated by [`agent.crawlers`](/docs/0.1/configuration#ai-crawlers). The docs runtime keeps
its other output, such as `/sitemap.md` and `/.well-known/llms.txt`.

## Last updated dates

When `lastUpdated` is enabled, Farm.js uses the latest Git commit for each markdown page instead of
trusting deployment file timestamps. Production builds preserve those dates in the bundled docs
content, so archive or copy metadata cannot turn into a misleading footer date.

Set an explicit date in frontmatter when a page needs editorial control:

```md
---
title: "Release Policy"
lastModified: "2026-07-16"
---
```

Explicit frontmatter wins over generated Git metadata. When Git history is unavailable, local docs
fall back to the source file timestamp and copied production docs fall back to the build date.

A shallow clone holds only recent history, and Git lists its oldest commit as adding every file. A
page last changed before that commit therefore shows no date, instead of the oldest commit's date
or the build date, and `farm build` warns how many pages it left undated. Build from a full clone
(`git fetch --unshallow`) to date every page. Vercel clones 10 commits deep; the
`VERCEL_DEEP_CLONE=true` environment variable is widely reported to give a full clone, but Vercel
does not document it, so check a deployment's dates after setting it.

## Override behavior

When `docs.entry` is enabled in `farm.config.ts`, Farm.js can mount docs pages and docs API routes automatically. Add explicit route wrappers only when the app wants to override default rendering, authentication, or response behavior.

```ts
// src/app/api/docs/route.ts
import { createDocsAPI } from "@farm.js/core/docs";

export const { GET, POST } = createDocsAPI();
```

Add the same exports to `src/app/api/docs/[...docs]/route.ts` when the override should also own
path-style machine routes. Most applications do not need either wrapper.

## Prerender docs pages

Docs pages render on request by default. Set `docs.prerender` to have `farm build` render them into
static files on Node and serverless targets:

```ts
export default defineConfig({
  docs: {
    entry: "/docs",
    prerender: true,
    socialImage: { baseUrl: "https://docs.example.com" },
  },
});
```

Farm.js's built-in renderer (`docs.adapter: false`) prerenders every page in the docs content
directory. A docs adapter such as `@farming-labs/farmjs` prerenders the pages it lists; an adapter
that does not list its pages keeps rendering them on request, with a build warning.

A built-in page's social metadata (`og:url`, `og:image`) is an absolute URL. Without
`socialImage.baseUrl` its origin is the request's, which a static file would freeze at the build's,
so those pages keep rendering on request and the build says how many. Set `socialImage.baseUrl`, or
`socialImage: false` in a page's frontmatter, to prerender them.

A docs page is a static file once prerendered, so it does not negotiate `Accept: text/markdown`;
agents use the `.md` URL the page links to. Markdown, search, `llms.txt`, and MCP keep rendering on
request. A page that middleware, a redirect, a rewrite, i18n, or a dynamic or cached route rule
covers keeps rendering on request, as for static app pages; `routeRules: { "/docs/changelog": {
prerender: false } }` keeps one page dynamic. Edge targets already serve docs pages from the build's
docs snapshot.

On Vercel, docs a sign-in guard covers can prerender too: give the guard's `middleware.ts`
`export const config = { runtime: "edge" }`, and it runs before Vercel serves each page's file. The
built-in renderer already keeps guarded pages out of search, `llms.txt`, and the sitemaps. See
[Edge middleware on Vercel](/docs/0.1/middleware#edge-middleware-on-vercel).

## Production notes

- Keep docs content in markdown so human pages and agent-readable pages stay in sync.
- Vercel functions bundle the docs content and last-modified metadata beside the server entry. Verify the emitted function without access to the source checkout so local files cannot hide a missing deployment asset.
- Keep the canonical docs configuration in the `docs` property of `farm.config.ts`.
- Use an external docs config only when a large navigation or theme definition is easier to maintain separately.
- Edge output snapshots docs content during `farm build`; rebuild the app after changing a page or docs configuration. `farm dev` continues to read source files live.
- Edge output supports Farm.js's built-in renderer (`docs.adapter: false`) and external adapters that publish an `edgeCompiler` entry. The current `@farming-labs/farmjs` adapter publishes that compiler automatically; applications do not configure it themselves. The capability is additive, so older Farm.js releases ignore it and retain their existing Node behavior.
- The official adapter's edge snapshot covers pages, client navigation, CSS, Markdown, search, sitemap, robots, and agent discovery. Set `mcp: false` and `telemetry: false` in the docs configuration because both features are enabled by default for production Node runtimes. Runtime-only adapter features such as docs MCP, Ask AI, feedback callbacks, analytics, telemetry, observability, and localized filesystem trees still require a Node target. Farm.js stops the build with the unsupported feature names instead of silently dropping them.
- Built-in and serializable hosted search providers run on edge targets. A function-backed custom search adapter requires a Node target because functions cannot be embedded in the content snapshot.
- Keep generated docs routes public unless product docs require auth.
- Verify docs build output before publishing package docs.

## Sitemap

See the full [sitemap](/sitemap.md) for all pages.
Docs-scoped sitemap: [/docs/sitemap.md](/docs/sitemap.md).
Well-known sitemap: [/.well-known/sitemap.md](/.well-known/sitemap.md).
