-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
50 lines (43 loc) · 1.62 KB
/
Copy pathmiddleware.ts
File metadata and controls
50 lines (43 loc) · 1.62 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
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
export async function middleware(request: NextRequest) {
// 1. URL parameter cleanup (SEO Canonicalization)
// 301 Redirect to strip referral parameters and consolidate link equity
if (request.nextUrl.searchParams.has('ref')) {
const cleanUrl = request.nextUrl.clone()
cleanUrl.searchParams.delete('ref')
return NextResponse.redirect(cleanUrl, 301)
}
// 2. Intercept requests asking for Markdown (Agent/Crawler Negotiation)
const acceptHeader = request.headers.get('accept') || ''
if (acceptHeader.includes('text/markdown')) {
try {
// For the homepage, we serve the comprehensive llms.txt file natively as markdown
if (request.nextUrl.pathname === '/') {
const url = new URL('/llms.txt', request.url)
const response = await fetch(url.toString())
if (response.ok) {
const text = await response.text()
return new NextResponse(text, {
status: 200,
headers: {
'Content-Type': 'text/markdown; charset=utf-8',
'x-markdown-tokens': 'estimated-2048' // Optional signal for agents
}
})
}
}
} catch (e) {
// Fail silently and fallback to standard HTML
return NextResponse.next()
}
}
// Continue standard HTML processing for browsers
return NextResponse.next()
}
// Match all routes except statics/api
export const config = {
matcher: [
'/((?!api|_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp|avif|ico|xml|txt)).*)',
],
}