Next.js App Router GEO: llms.txt & Crawler Setup 2026
Implement /llms.txt via a Next.js route handler, govern AI crawlers in middleware, and validate with AI crawler checkers to avoid SPA crawl gaps and improve GEO readiness.
Direct answer: To optimize a Next.js App Router site for generative engine optimization (GEO), generate a dynamic /llms.txt using a route handler, enforce crawler access rules for GPTBot and ClaudeBot in middleware, and validate with AI crawler checkers. This ensures AI agents can discover and parse your content without SPA crawl gaps.
Why Next.js App Router Needs Explicit GEO Configuration
Next.js App Router defaults to server-side rendering (SSR) and streaming, which is beneficial for AI crawlers that may not execute JavaScript. However, without explicit governance, AI crawlers like GPTBot and ClaudeBot may be blocked by default robots.txt rules or fail to discover key resources like /llms.txt. The 2026 WorkAgent GEO 200-Site Index shows that top-performing sites (e.g., Supabase, Cloudflare, Resend, Mintlify) all score 100/100 with /llms.txt present, JSON-LD structured data, SSR enabled, and GPTBot/OAI-SearchBot allowed. In contrast, Perplexity scores 44/100 despite allowing GPTBot, due to missing /llms.txt, JSON-LD, and SSR. This gap highlights the need for deliberate configuration in Next.js App Router projects.
Step 1: Generate /llms.txt with a Route Handler
The /llms.txt file is a emerging standard that provides AI crawlers with a concise, machine-readable summary of your site's content and policies. In Next.js App Router, create a route handler at app/llms.txt/route.ts (or app/llms.txt/route.js). This handler should return a plain text response with the appropriate Content-Type: text/plain header.
```typescript
// app/llms.txt/route.ts
import { NextResponse } from 'next/server';
export async function GET() {
const content = `# llms.txt for example.com
This file provides AI crawlers with a summary of our site.
User-agent: *
Allow: /
Sitemap
Sitemap: https://example.com/sitemap.xml
Contact
Contact: mailto:ai@example.com
Preferred content
We welcome AI crawlers to index our public pages.
For questions, contact ai@example.com.
`;
return new NextResponse(content, {
status: 200,
headers: {
'Content-Type': 'text/plain',
'Cache-Control': 'public, max-age=86400, s-maxage=86400',
},
});
}
```
This handler dynamically generates the file at request time, allowing you to include environment-specific data or fetch from a CMS. Ensure the route is publicly accessible and not blocked by middleware (see Step 2). For static generation, you can use generateStaticParams or export a static file in public/llms.txt, but dynamic generation is preferred for maintainability.
Step 2: Configure Middleware for AI Crawler Governance
Next.js middleware runs before every request and can be used to allow or block specific user agents. Create or modify middleware.ts at the project root to explicitly allow GPTBot and ClaudeBot while blocking unwanted bots. The following example allows these AI crawlers and sets a custom header for logging.
```typescript
// middleware.ts
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
export function middleware(request: NextRequest) {
const userAgent = request.headers.get('user-agent') || '';
const allowedBots = ['GPTBot', 'ClaudeBot', 'OAI-SearchBot'];
const isAllowedBot = allowedBots.some(bot => userAgent.includes(bot));
// Allow AI crawlers to access all routes
if (isAllowedBot) {
const response = NextResponse.next();
response.headers.set('x-ai-crawler', 'allowed');
return response;
}
// Optional: block other bots or enforce rate limiting
// ...
return NextResponse.next();
}
export const config = {
matcher: '/:path*', // Apply to all routes
};
```
This middleware ensures that AI crawlers are not inadvertently blocked by other rules. It also adds a header for observability. Note that middleware runs on the Edge runtime by default, so avoid Node.js-specific APIs. For more granular control, you can combine this with robots.txt rules.
Step 3: Validate with AI Crawler Checkers
After deploying, validate that /llms.txt is accessible and that AI crawlers are allowed. Use online tools or command-line checks:
- cURL:
curl -I https://yourdomain.com/llms.txtshould return200 OKandContent-Type: text/plain. - AI Crawler Checker: Tools like the WorkAgent GEO audit (see link at end) or third-party services can simulate GPTBot requests and verify access.
- robots.txt Tester: Ensure
robots.txtdoes not disallow/llms.txtor AI crawlers.
Additionally, verify that your site uses SSR for critical pages. The benchmark data shows that all 100/100 sites have SSR enabled, while Perplexity (44/100) lacks SSR. In Next.js App Router, pages are server-rendered by default, but client components may introduce JavaScript dependencies. Use generateMetadata and server components to ensure content is available without JS.
Comparison: GEO Readiness Checklist for Next.js App Router
| Criterion | Required | How to Implement in Next.js App Router |
|---|---|---|
/llms.txt present | Yes | Route handler at app/llms.txt/route.ts |
| JSON-LD structured data | Yes | Add <script type="application/ld+json"> in layout or page |
| SSR enabled | Yes | Use server components; avoid client-only rendering for critical content |
| GPTBot allowed | Yes | Middleware allowlist or robots.txt |
| OAI-SearchBot allowed | Yes | Middleware allowlist or robots.txt |
| TTFB < 2000ms | Recommended | Optimize server response; benchmark shows 1554–2917ms for top sites |
Honest Scope Boundaries
This guide focuses on technical configuration for AI crawler access and /llms.txt generation. It does not guarantee improved rankings in AI-generated answers, as those depend on content quality, authority, and model-specific factors. The benchmark data is from a 200-site index and may not generalize to all industries. Implementing these steps reduces crawl gaps but should be part of a broader GEO strategy including content optimization and structured data.
For a free audit of your Next.js site's GEO readiness, visit WorkAgent GEO Audit.
Frequently Asked Questions (FAQPage Schema)
- Q1: How do I add llms.txt to a Next.js App Router project?
- Create a route handler at app/llms.txt/route.ts that returns a plain text response with your llms.txt content, ensuring it's publicly accessible and not blocked by middleware.
- Q2: How can I configure AI crawler access in Next.js middleware?
- In middleware.ts, check the User-Agent header for GPTBot, ClaudeBot, or OAI-SearchBot and allow the request by returning NextResponse.next(); optionally add headers for logging.
- Q3: Why is my Next.js site not being crawled by AI bots?
- Common causes include robots.txt disallow rules, middleware blocking, or client-side rendering that hides content; ensure /llms.txt exists, middleware allows AI bots, and critical pages use SSR.
Check your website’s AI crawler readiness free in 30 seconds
Run a free, no-login check of robots.txt rules, /llms.txt, HTML readability, and JSON-LD — then sign up free (14 instant credits) to generate industry-matched GEO articles.