llms.txt Next.js Guide: App Router Route Handler
In Next.js App Router, you can serve /llms.txt either as a static file in public/llms.txt or from a Route Handler at app/llms.txt/route.ts. Using a Route Handler lets you pull titles and URLs from your CMS or MDX folder automatically while still caching at the edge.
Add an App Router llms.txt Route Handler
Next.js App Router supports dots in folder names, so creating app/llms.txt/route.ts maps directly to https://yourdomain.com/llms.txt without custom rewrites. Export dynamic = 'force-static' (or an ISR revalidate interval) so the response is cached instead of running serverless compute on every bot request.
export const dynamic = 'force-static';
export const revalidate = 3600;
export async function GET() {
const origin = process.env.NEXT_PUBLIC_SITE_URL || 'https://example.com';
const body = [
'# Acme Next.js App',
'',
'> Developer docs and API reference for Acme.',
'',
'## Documentation',
`- [Quickstart](${origin}/docs/quickstart): Install and configure the SDK`,
`- [API Reference](${origin}/docs/api): Endpoints, auth and rate limits`,
'',
'## Optional',
`- [Changelog](${origin}/changelog): Recent releases`,
'',
].join('\n');
return new Response(body, {
headers: {
'Content-Type': 'text/plain; charset=utf-8',
'Cache-Control': 'public, max-age=3600, s-maxage=86400',
},
});
}# For small sites, download llms.txt from the Studio and save directly:
mkdir -p public
cp ~/Downloads/llms.txt public/llms.txtAvoid Common Next.js Middleware Traps
- Exclude
llms.txtfrom auth or locale middleware — update yourmiddleware.tsmatcher regex so/llms.txtand/llms-full.txtare never redirected to/en/llms.txtor a login screen. - Use absolute HTTPS URLs — derive URLs from
NEXT_PUBLIC_SITE_URLrather than hardcodedlocalhost:3000. - Validate the live response — paste your rendered output into the llms.txt Studio to check H1, summary and link syntax.
Next.js llms.txt Questions
Does app/llms.txt/route.ts Work With Next.js Static Export?
Yes. As long as you export const dynamic = "force-static", next build with output: "export" writes a static llms.txt file into the out/ directory.
Should I Also Add app/llms-full.txt/route.ts?
Yes if your docs live in local MDX or a headless CMS. Follow the same folder naming pattern (app/llms-full.txt/route.ts) and concatenate your stripped Markdown pages into one cached text/plain response.