Skip to content

Commit 9b3c842

Browse files
committed
if an ai requests the docs, send them llms.txt
1 parent 6e606c9 commit 9b3c842

1 file changed

Lines changed: 41 additions & 1 deletion

File tree

examples/web/vite.config.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,49 @@ import { defineConfig } from 'vite'
33
import path from 'path'
44
import fs from 'fs'
55

6+
const llmsTxtPath = path.resolve(__dirname, '../../llms.txt')
7+
8+
/**
9+
* Vite plugin that serves llms.txt for API requests to the root page.
10+
* When a request to "/" comes in without an "Accept: text/html" header
11+
* (e.g., curl, LLM agents, API clients), we return the llms.txt content
12+
* instead of the SPA HTML.
13+
*/
14+
function llmsTxtPlugin() {
15+
return {
16+
name: 'llms-txt',
17+
configureServer(server: any) {
18+
server.middlewares.use((req: any, res: any, next: any) => {
19+
const isLlmsTxtPath = req.url === '/llms.txt'
20+
const isRoot = req.url === '/' || req.url === ''
21+
22+
if (!isLlmsTxtPath && !isRoot) {
23+
return next()
24+
}
25+
26+
// Always serve llms.txt at /llms.txt, and at / only for non-browser requests
27+
if (isRoot) {
28+
const accept = req.headers['accept'] || ''
29+
if (accept.includes('text/html')) {
30+
return next()
31+
}
32+
}
33+
34+
try {
35+
const content = fs.readFileSync(llmsTxtPath, 'utf-8')
36+
res.setHeader('Content-Type', 'text/plain; charset=utf-8')
37+
res.end(content)
38+
} catch {
39+
next()
40+
}
41+
})
42+
},
43+
}
44+
}
45+
646
// https://vitejs.dev/config/
747
export default defineConfig({
8-
plugins: [vue()],
48+
plugins: [llmsTxtPlugin(), vue()],
949
server: {
1050
port: 5050,
1151
open: true,

0 commit comments

Comments
 (0)