Repository navigation
Expand file tree
/
Copy pathproxy.ts
More file actions
98 lines (86 loc) · 2.94 KB
/
Copy pathproxy.ts
File metadata and controls
98 lines (86 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { NextRequest, NextResponse } from "next/server";
/**
* Known LLM/AI user agent patterns
* These agents get raw markdown automatically
*/
const LLM_USER_AGENTS = [
"ChatGPT-User", // OpenAI ChatGPT browsing
"GPTBot", // OpenAI crawler
"Claude-Web", // Anthropic Claude browsing
"ClaudeBot", // Anthropic crawler
"PerplexityBot", // Perplexity AI
"Applebot", // Apple Intelligence/Siri
"cohere-ai", // Cohere
"anthropic-ai", // Anthropic
"Google-Extended", // Google AI (Bard/Gemini)
"CCBot", // Common Crawl (used by many LLMs)
];
/**
* Check if user agent is an LLM/AI agent
*/
function isLLMUserAgent(userAgent: string | null): boolean {
if (!userAgent) return false;
return LLM_USER_AGENTS.some(bot =>
userAgent.toLowerCase().includes(bot.toLowerCase())
);
}
/**
* Proxy to serve raw markdown for AI agents
*
* Routes to raw markdown API when:
* 1. URL ends with .md extension (e.g., /api.md)
* 2. Accept header includes text/markdown
* 3. Query param ?format=md is present
* 4. User-Agent is a known LLM (ChatGPT, ClaudeBot, PerplexityBot, etc.)
*
* This allows AI agents to fetch documentation as raw markdown
* while browsers get the rendered HTML version.
*/
export function proxy(request: NextRequest) {
const { pathname } = request.nextUrl;
// Skip API routes, static files, and Next.js internals
if (
pathname.startsWith("/api/") ||
pathname.startsWith("/_next/") ||
pathname.startsWith("/static/") ||
pathname.startsWith("/_pagefind/") ||
// Skip files with extensions (except .md) - check last segment after last slash
(/\/[^/]+\.[^/.]+$/.test(pathname) && !pathname.endsWith(".md"))
) {
return NextResponse.next();
}
const userAgent = request.headers.get("User-Agent");
// Check if this is a docs page request that wants markdown
const wantsMarkdown =
pathname.endsWith(".md") ||
request.headers.get("Accept")?.includes("text/markdown") ||
request.nextUrl.searchParams.get("format") === "md" ||
isLLMUserAgent(userAgent);
if (wantsMarkdown) {
// Normalize the path (remove .md extension if present)
let docPath = pathname.replace(/\.md$/, "");
// Handle root path
if (docPath === "" || docPath === "/") {
docPath = "/index";
}
// Preserve query params except format
const url = new URL(request.url);
url.pathname = `/api/docs${docPath}`;
url.searchParams.delete("format");
// Rewrite to the API route (internal redirect, URL doesn't change for client)
return NextResponse.rewrite(url);
}
return NextResponse.next();
}
// Only run proxy on relevant paths
export const config = {
matcher: [
/*
* Match all paths except:
* - API routes (already handled)
* - Static files with extensions (images, css, js, etc.)
* - Next.js internals
*/
"/((?!api|_next/static|_next/image|_pagefind|favicon.ico|.*\\.[^m][^d]$).*)",
],
};