How to add llms.txt to Next.js
Next.js makes this trivial: drop the file in /public or expose a route handler that streams plain text. Both options take under five minutes.
Before you start
- ✓A Next.js app (Pages or App Router)
- ✓Deploy permissions
- ✓An llms.txt body ready to ship
Install in 3 steps
- 1
Static option
Put llms.txt in /public. Next will serve it at /llms.txt automatically with the right content-type.
- 2
Dynamic option (App Router)
Create app/llms.txt/route.ts that returns new Response(content, { headers: { 'Content-Type': 'text/plain' } }).
- 3
Verify
Run next build and curl your deployed URL. Make sure no middleware rewrites the request.
Troubleshooting
middleware.ts is intercepting /llms.txt. Add a matcher exclusion: matcher: ['/((?!llms.txt).*)'].
Use pages/api/llms.ts returning the body, plus a next.config.js rewrite from /llms.txt to /api/llms.
Common gotchas
- • middleware.ts can intercept root paths — add a matcher exclusion
- • If you're on the Pages Router, use pages/api/llms.ts + a rewrite in next.config.js
Generate your llms.txt in 30 seconds
Use the free OptimAIze generator, then follow the steps above to deploy on Next.js.
Frequently asked questions
Does Next.js need both llms.txt and robots.txt?
Yes. robots.txt grants crawler permission; llms.txt curates which pages matter. Together they form the minimum AI-search setup on any Next.js site.
Will adding llms.txt slow down my site?
No. It's a tiny static text file fetched once and cached aggressively. Page-load impact is effectively zero.
How often should I update llms.txt?
Whenever your canonical content set changes — new docs section, new product line, new pricing page. A monthly review is a sensible cadence for most sites.
Can I see whether AI engines read my llms.txt?
Check your server logs for user agents like GPTBot, ClaudeBot, PerplexityBot, and Google-Extended hitting /llms.txt. Most sites see traffic within days of publishing.