LLMs.txt Tools
All tools are free to use — no account and no sign-up required.
Back to the generator

llms.txt V2 on Next.js

The second revision of llms.txt adds two things an AI reader values most: a Markdown version of each page, and an explicit link that points to it. This guide wires both into a Next.js App Router site — a Markdown route handler, rewrites that catch .md and Accept: text/markdown requests, the two V2 link relations, and a check at the end.

Published: [FILL IN] · Last modified: [FILL IN]

What you'll build

V2 keeps the llms.txt file itself exactly as it was. What it adds is a reliable way for an agent to fetch clean Markdown instead of your rendered HTML, and two link relations that advertise where that Markdown lives. Here is each piece and how it maps to Next.js.

V2 featureHow it is done in Next.js
llms.txt itselfA static file in /public, or a route handler from the base Next.js guide
Markdown URL pattern page.mdOne catch-all route handler at app/md/[...slug]/route.ts
Markdown URL pattern page.html.mdThe same handler — strip the trailing .md and map the rest to your content folder
Accept: text/markdownA rewrite whose has condition matches the Accept header
alternate link, type text/markdownA Link response header, plus metadata.alternates.types
describedby linkA link element added in app/layout.tsx
Per-subfolder llms.txtFiles under public/, such as public/docs/llms.txt
## Optional has no special meaningKeep using it as an ordinary heading to group links

The snippets below are illustrative for your own site. This project's own build does not use runtime or dynamic route-segment config exports, so the examples stay with a plain route handler and standard rewrites.

Step 1: A Markdown route handler

Start with one catch-all handler that returns Markdown with the right content type. Put it at app/md/[...slug]/route.ts; Next.js maps the [...slug] segment to an array of path pieces, which you join onto a content folder. Returning the file as text/markdown tells the client how to read it, and Vary: Accept tells caches that the response depends on the request's Accept header.

app/md/[...slug]/route.ts
import { NextResponse } from "next/server";
import { readFile } from "node:fs/promises";
import path from "node:path";

const ROOT = path.join(process.cwd(), "content");

export async function GET(
  _request: Request,
  { params }: { params: Promise<{ slug: string[] }> },
) {
  const { slug } = await params;

  // Path-traversal guard: refuse any ".." segment.
  if (slug.some((segment) => segment === "..")) {
    return new NextResponse("Not found", { status: 404 });
  }

  const filePath = path.join(ROOT, ...slug);
  const markdown = await readFile(filePath, "utf8").catch(() => null);

  if (markdown === null) {
    return new NextResponse("Not found", { status: 404 });
  }

  return new NextResponse(markdown, {
    headers: {
      "content-type": "text/markdown; charset=utf-8",
      vary: "Accept",
    },
  });
}

The guard matters: because the slug comes from the URL, reject any .. segment before touching the filesystem, so a request cannot read files outside your content folder. Returning a plain 404 for a missing file keeps the handler small.

Step 2: Rewrites for .md and Accept: text/markdown

Now route the public URLs a reader or agent actually requests into that handler. The two patterns V2 cares about are an explicit .md suffix and content negotiation through the Accept header. Both belong in beforeFiles, so they are checked before the filesystem and win over any static file of the same name.

next.config.js
/** @type {import('next').NextConfig} */
const nextConfig = {
  async rewrites() {
    return {
      // Checked before the filesystem, so these win over static files.
      beforeFiles: [
        // 1. Explicit Markdown URLs: /docs/api.md -> /md/docs/api
        {
          source: "/:path*.md",
          destination: "/md/:path*",
        },
        // 2. Content negotiation: Accept: text/markdown -> /md/<same path>
        {
          source: "/:path*",
          has: [{ type: "header", key: "accept", value: ".*text/markdown.*" }],
          destination: "/md/:path*",
        },
      ],
      afterFiles: [],
      fallback: [],
    };
  },
};

module.exports = nextConfig;

The first rule takes any path ending in .md and strips that suffix, so /docs/api.md resolves to the handler with the slug ["docs", "api"]. The second rule matches when the client sends an Accept header that includes text/markdown. Keep the explicit .md rule first so a literal file request is never swallowed by the broader match.

Step 3: The two V2 links

V2 defines two link relations. The alternate relation, with type text/markdown, points at the Markdown version of a page; the describedby relation points at the llms.txt that describes it. On Next.js you can emit the alternate in either of two ways — as a Link response header from the handler, or from page metadata so it is rendered into the document head.

Link header or metadata
// (a) A Link response header from the Markdown route handler.
export async function GET() {
  const body = "# Getting started\n\nInstall and run in minutes.";
  const markdownUrl = `</docs/getting-started.md>; rel="alternate"; type="text/markdown"`;

  return new NextResponse(body, {
    headers: {
      "content-type": "text/markdown; charset=utf-8",
      vary: "Accept",
      link: markdownUrl,
    },
  });
}

// (b) The same relation declared from page metadata.
export const metadata: Metadata = {
  alternates: {
    types: {
      "text/markdown": "/docs/getting-started.md",
    },
  },
};

Use the Link header when the Markdown is produced by a route handler and you want the relation to travel with the response. Use metadata.alternates.types on an ordinary page when the Markdown file already exists and you only need the head element. Both describe the same pair, so pick the one that matches where the content is generated.

Step 4: Link llms.txt to your .md pages

Finally, list the Markdown URLs in llms.txt so an agent can go straight to the clean version. Point each link at the .md address rather than the HTML page.

llms.txt
# Acme Docs
> Guides and API references for the Acme platform.

## Docs
- [Getting started](https://acme.com/docs/getting-started.md): Install and run in minutes.
- [API reference](https://acme.com/docs/api.md): Every endpoint and parameter.

If a folder needs its own index, V2 lets a subfolder carry its own llms.txt. On Next.js that is simply another file under /public, for example public/docs/llms.txt, and the most specific file applies to pages inside that folder.

Optional: a plugin

There is a community package, @turbodocx/next-plugin-llms, that aims to generate an llms.txt from your Next.js routes and can save some of the wiring above. Treat any plugin as a convenience, not a requirement — the plain handler and rewrites in this guide work without it.

Before adopting it, check that it is still maintained: look at the last release date, whether it supports your Next.js version, and whether its output matches the V2 URL patterns. If the project has gone quiet, the native approach is the safer choice.

Step 5: Check it with the V2 checker

With the handler and rewrites in place, confirm that each .md address returns Markdown and that your link relations are present. The llms.txt V2 checker walks through the V2 requirements and flags anything missing.

If you have not published the base llms.txt yet, start with the llms.txt on Next.js guide for the static file and route handler options, then come back here for the V2 additions.

Check your file

Validate your llms.txt before you go live — it scores nine checks and flags broken links. Open the LLMs.txt Validator.

Generate yours free

No account needed. Build an llms.txt from your website, sitemap or a pasted list in seconds. Open the LLMs.txt Generator.

Learn more