---
title: Next.js MDX content collections for AI-readable docs
description: Configure a local MDX content collection in Next.js, map files to routes, add useful frontmatter, and generate llms.txt, page Markdown, and JSON-LD.
canonical_url: https://nextaiready.com/en/docs/guides/mdx-content
url: https://nextaiready.com/en/docs/guides/mdx-content
last_updated: 2026-11-23
updated: 2026-11-23
author: next-ai-ready team
summary: Configure a local MDX content collection in Next.js, map files to routes, add useful frontmatter, and generate llms.txt, page Markdown, and JSON-LD.
topics: [nextjs, mdx, content-collections, frontmatter, llms.txt, ai-readable-docs]
---

# Next.js MDX content collections for AI-readable docs

An MDX content collection is a directory of Markdown or MDX source files that share a predictable schema and route convention. In a Next.js project, next-ai-ready can read that collection at build time and generate `llms.txt`, per-page Markdown, JSON-LD, and a searchable semantic graph without replacing the UI that already renders the content.

For the complete endpoint setup, start with the [Next.js llms.txt walkthrough](./nextjs-llms-txt).
Use this guide when you need to select source files, map routes, or diagnose a missing page.

The minimal workflow is:

1. Keep the source `.md` or `.mdx` files in the repository.
2. Initialize the integration and match those files with `content` globs in `ai-ready.config.*`.
3. Add clear frontmatter and direct answers to each page.
4. Run `next-ai-ready build` and verify the generated routes.

If you are setting up the SDK for the first time, follow the [Quickstart](./quickstart) through a successful Markdown response, then return here to connect your own content collection.

## Create a local MDX collection

From an existing Next.js App Router project, install and initialize the integration once:

```bash
pnpm add next-ai-ready
pnpm exec next-ai-ready init
```

Skip `init` if the project already has the AI handlers and `withAiReady()` wiring. A content
configuration alone lets the CLI build artifacts, but does not install the runtime Markdown routes.

A small documentation collection can use this structure:

```text
content/
  docs/
    introduction.mdx
    installation.mdx
    deployment.mdx
```

For the endpoint check below, create `content/docs/installation.mdx` with this content:

```markdown
---
title: Install Acme
summary: Install dependencies for an existing Acme checkout.
---

# Install Acme

Run `pnpm install` from the application root to install the dependencies.
```

Edit the generated `ai-ready.config.ts` (TypeScript) or `ai-ready.config.mjs` (JavaScript) to scan
the source collection. Use the existing file rather than creating a second config:

```js
// ai-ready.config.ts or ai-ready.config.mjs
import { defineConfig } from "next-ai-ready"

export default defineConfig({
  site: {
    name: "Acme Docs",
    baseUrl: "https://docs.example.com",
  },
  content: ["content/docs/**/*.{md,mdx}"],
})
```

Use the source files, not JavaScript generated by another content tool. That keeps URLs, authorship, and Markdown output tied to material a maintainer can review.

## Add frontmatter that survives every output

Start with a small schema that is useful in HTML, search results, and machine-readable artifacts:

```yaml
---
title: Deploy Acme on Vercel
summary: Build and deploy the Acme Next.js app, then verify its public health endpoint.
updatedAt: 2026-09-26
author: Acme engineering
tags:
  - nextjs
  - vercel
  - deployment
questions:
  - q: Which command creates the production build?
    a: Run pnpm build from the application root.
---
```

| Field       | Use it for                                                     |
| ----------- | -------------------------------------------------------------- |
| `title`     | Page heading, `llms.txt`, JSON-LD, and search result title.    |
| `summary`   | Search description, page discovery, and concise agent context. |
| `updatedAt` | Visible freshness and `dateModified` metadata.                 |
| `author`    | Authorship in generated structured data.                       |
| `tags`      | Topics used by the semantic graph and page search.             |
| `questions` | Direct answers and optional `FAQPage` JSON-LD.                 |

Only publish metadata the visible page supports. A large generated FAQ block does not make thin content more useful.

## Understand route mapping

The default filesystem source derives a route from each matched path. Directories named `app`, `src/app`, `content`, and `src/content` are treated as source roots.

| Source file                        | Generated route         |
| ---------------------------------- | ----------------------- |
| `content/index.mdx`                | `/`                     |
| `content/docs/installation.mdx`    | `/docs/installation`    |
| `content/en/docs/installation.mdx` | `/en/docs/installation` |

Run the build and inspect the graph before deployment:

```bash
pnpm exec next-ai-ready build
pnpm exec next-ai-ready doctor --score
```

The mapping is ready when it appears in `public/llms.txt` and `.next-ai-ready/graph.json` with the
expected canonical URL. Creating files under `content/` does not create browser pages; your
existing MDX renderer must serve the corresponding HTML routes.

## Use an existing MDX stack

next-ai-ready is a machine-facing build layer, not a replacement for the component system that renders your pages.

- **Next.js with `@next/mdx`:** include routed MDX files with an `app/**/*.{md,mdx}` glob.
- **A local content collection:** scan the original `content/**/*.{md,mdx}` files.
- **Fumadocs:** keep its source configuration and follow the [Fumadocs integration guide](./fumadocs-ai-ready).
- **Nextra:** keep Nextra's theme and routing, then follow the [Nextra integration guide](./nextra-ai-ready).
- **Generated collection tools:** scan the original MDX directory instead of a generated `.content` or JavaScript output directory.

If the browser UI and the source collection use different route rules, add a custom content source adapter instead of publishing incorrect canonical URLs.

## Add semantic details when they are real

Frontmatter is enough for most pages. A code-owned source can also pass richer semantic data to the
compiler:

```ts
export const semantic = {
  topics: ["install", "quickstart", "pnpm"],
  questions: [
    { q: "How do I install Acme?", a: "Run `pnpm add acme`." },
  ],
  entities: [
    { name: "pnpm", type: "tool", url: "https://pnpm.io" },
  ],
}
```

The compiler extracts headings, sections, FAQs, entities, and token-aware chunks deterministically. It does not call an external model or require a database.

## Verify the public result

`init` adds `next-ai-ready build` to the app's build script. Build the app and start the production
server:

```bash
pnpm build
pnpm start
```

In another terminal, check the file created above:

```bash
curl -i http://localhost:3000/llms.txt
curl -i http://localhost:3000/docs/installation.md
```

Expect `200` and `Content-Type: text/markdown` for the page, with the `Install Acme` title and its
dependency-installation instructions, not a missing-page recovery document. If the UI serves
`/docs/installation`, check that its normal browser response is still HTML.

After deployment, run the audit against the real origin:

```bash
pnpm exec next-ai-ready audit https://docs.example.com --version 3
```

Confirm that normal page URLs still return HTML, `.md` URLs return Markdown, and generated canonical URLs use the production domain.

## Common content collection problems

### The collection is empty

**Symptom:** a successful build leaves the intended page out of `public/llms.txt` and
`.next-ai-ready/graph.json`. The build may not be scanning your source directory.

**Fix:** run from the application root containing `ai-ready.config.*` and match the actual source
files, not another tool's generated JavaScript directory. For the example above, use
`content/docs/**/*.{md,mdx}` and confirm `content/docs/installation.mdx` exists and is not excluded.
Rebuild the app and start a new server process; the running handler caches its graph:

```bash
pnpm build
pnpm start
```

In another terminal:

```bash
curl -i http://localhost:3000/llms.txt
curl -i http://localhost:3000/docs/installation.md
```

**Verify:** the index lists `/docs/installation`, and the page returns `text/markdown` with the
`Install Acme` title and dependency-installation instructions. A `200` response containing
`document_status: "not_found"` is still a missing page; use the [endpoint troubleshooting
steps](./nextjs-llms-txt) if the graph is correct but retrieval is not.

### Production output contains local URLs

**Symptom:** an index URL, page canonical, or Markdown canonical `Link` header points to
`localhost` or a development host after deployment.

**Fix:** set `site.baseUrl` in the config used by the production build to the real public origin.
Rebuild and redeploy that artifact; changing config without a new build leaves the generated URLs
unchanged. Replace this example origin with your own domain:

```bash
curl -i https://docs.example.com/llms.txt
curl -i https://docs.example.com/docs/installation.md
```

**Verify:** the index and the page's canonical `Link` header use the production origin, and the
Markdown body is the intended page. If your renderer serves the HTML route, check its canonical
too. Setting `site.baseUrl` does not configure a domain, fix a wrong route, or guarantee indexing.

- **Routes contain `content/` or `src/app/`.** Upgrade the package and verify the source-root rules.
- **The UI route differs from the graph route.** Add a content source adapter with explicit routes.
- **Private drafts appear in `llms.txt`.** Exclude drafts from the matched source; generated artifacts are public unless the deployment protects them.
- **The page ranks for the wrong query.** Make the title, summary, H1, and first paragraph answer the same specific problem instead of adding more tags.

For a complete installation, continue with the [installation reference](../installation) and verify the result with `doctor` before deploying.
