§ LLMS.FREELLMS.TXT STUDIOVERIFIED 2026

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.

§ 01 · APP ROUTERFORCE-STATIC ROUTE HANDLERNEXT.JS

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',
    },
  });
}
§ 02 · DEPLOYMENT NOTESMIDDLEWARE AND ROBOTSGOTCHAS

Avoid Common Next.js Middleware Traps

  • Exclude llms.txt from auth or locale middleware — update your middleware.ts matcher regex so /llms.txt and /llms-full.txt are never redirected to /en/llms.txt or a login screen.
  • Use absolute HTTPS URLs — derive URLs from NEXT_PUBLIC_SITE_URL rather than hardcoded localhost:3000.
  • Validate the live response — paste your rendered output into the llms.txt Studio to check H1, summary and link syntax.
§ FAQCOMMON QUESTIONSANSWERS

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.