How to add llms.txt to Next.js
Next.js is the easy platform — you fully control the domain root. AI crawlers fetch https://yoursite.com/llms.txt, and there are three clean ways to serve it. Pick based on whether the content is static or generated.
Generate the file first — paste your site URL and get a spec-compliant llms.txt from your real pages, then drop it in with one of the methods below.
Generate my llms.txt →Method A — Static file in /public (simplest)
Drop the file at public/llms.txt. Next.js serves everything in /public from the root, so it's live at /llms.txt with the correct text/plain type. Add public/llms-full.txt the same way if you publish the full version.
Method B — Route handler (App Router, generated)
Want the file built from your own data? Add app/llms.txt/route.ts:
// app/llms.txt/route.ts
export const dynamic = "force-static";
export function GET() {
const body = `# Example Co
> Open-source analytics for indie developers.
## Docs
- [Quickstart](https://example.com/docs/quickstart): install in 5 minutes
- [API reference](https://example.com/docs/api): every endpoint
`;
return new Response(body, {
headers: { "content-type": "text/plain; charset=utf-8" },
});
}Build the body string from your CMS, MDX frontmatter, or the same source your sitemap uses so the two never drift apart.
Method C — API route (Pages Router)
On the older Pages Router, the equivalent is an API route plus a rewrite, since /llms.txt isn't a valid file-based page name:
// pages/api/llms.ts
import type { NextApiRequest, NextApiResponse } from "next";
export default function handler(_req: NextApiRequest, res: NextApiResponse) {
res.setHeader("content-type", "text/plain; charset=utf-8");
res.send("# Example Co\n\n> ...");
}
// next.config.js
module.exports = {
async rewrites() {
return [{ source: "/llms.txt", destination: "/api/llms" }];
},
};What to watch for
- Middleware — if you run
middleware.ts, exclude/llms.txtfrom its matcher so it isn't rewritten or auth-gated. - Content type — a route handler must set
text/plain; the default for a Response istext/html. - Don't block AI bots — serving the file is pointless if your host or CDN blocks GPTBot / ClaudeBot. Check your robots.txt and CDN bot rules.
Generate llms.txt and llms-full.txt from your URL, then ship it with Method A, B, or C.
Try the free llms.txt generator →Related: How to create llms.txt · llms.txt for WordPress · llms.txt for Shopify · vs sitemap.xml