Adding llms.txt to Next.js
Published:
Next.js makes this easy: the file just has to be served at https://yoursite.com/llms.txt as plain text. There are two clean approaches.
Option 1 — the public/ folder (simplest)
Anything in public/ is served from the site root. Write your file (see how to create an llms.txt file) and save it as public/llms.txt:
my-app/
public/
llms.txt # served at https://yoursite.com/llms.txt
app/
package.json
No code, no config. Next.js serves it with Content-Type: text/plain automatically.
Option 2 — a route handler (for generated content)
If you'd rather build the file from data (your sitemap, CMS, or MDX), add a route. In the App Router, create app/llms.txt/route.ts:
export function GET() {
const body = "# My App\n\n> Short summary.\n\n## Docs\n- [Start](https://myapp.com/start): Get going\n";
return new Response(body, {
headers: { "Content-Type": "text/plain; charset=utf-8" },
});
}
Watch out for these
- Root location: it must resolve at
/llms.txt, not/public/llms.txtor a subpath. - Plain text: serve as
text/plain, never HTML. - Rewrites: if you use middleware or rewrites, make sure they don't intercept
/llms.txt.
Verify it
Deploy, then run your domain through the validator — it confirms the structure and that every linked page actually loads.