Getting Started
Add AI-readable Markdown to your Next.js site in 3 minutes. When an AI agent requests any page with Accept: text/markdown or ?v=md, it gets clean Markdown with structured frontmatter instead of raw HTML.
Prerequisites
- Node.js 20 or later
- Next.js 16 or later (App Router)
Install
- pnpm
- yarn
- npm
- bun
pnpm add next-markdown-mirror
yarn add next-markdown-mirror
npm install next-markdown-mirror
bun add next-markdown-mirror
Setup
You need 3 files to wire everything up: a proxy, a route handler, and an llms.txt endpoint.
1. Proxy — proxy.ts
Create proxy.ts in your project root (next to next.config.ts). This intercepts incoming requests from AI agents and rewrites them to the route handler.
import { withMarkdownMirror } from 'next-markdown-mirror/nextjs';
export const proxy = withMarkdownMirror();
2. Route handler — app/md-mirror/[...path]/route.ts
This handler receives the rewritten request, fetches your page's HTML internally, converts it to Markdown, and returns it.
import { createMarkdownHandler } from 'next-markdown-mirror/nextjs';
export const GET = createMarkdownHandler({
baseUrl: process.env.NEXT_PUBLIC_SITE_URL!,
});
3. llms.txt — app/llms.txt/route.ts
Serve an llms.txt file so AI agents can discover your site's content.
import { createLlmsTxtHandler } from 'next-markdown-mirror/nextjs';
export const GET = createLlmsTxtHandler({
siteName: 'My Site',
baseUrl: process.env.NEXT_PUBLIC_SITE_URL!,
pages: [
{ url: '/', title: 'Home', description: 'Welcome page' },
{ url: '/about', title: 'About' },
],
});
Verify
Start your dev server, then test with curl:
# Request Markdown via Accept header
curl -H "Accept: text/markdown" http://localhost:3000/
# Request Markdown via query parameter
curl "http://localhost:3000/?v=md"
# Check llms.txt
curl http://localhost:3000/llms.txt
You should see clean Markdown with YAML frontmatter and a x-markdown-tokens response header.
Next steps
- How It Works — understand the request flow and detection logic
- Configuration — customize content selectors, excluded paths, and more
- llms.txt Protocol — set up AI discovery with sitemap support
- API Reference — full reference for every export