-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
100 lines (87 loc) · 2.68 KB
/
Copy pathproxy.ts
File metadata and controls
100 lines (87 loc) · 2.68 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
99
100
import { NextRequest, NextResponse } from 'next/server';
// Map routes to their API markdown endpoints
const MARKDOWN_ROUTES: Record<string, string> = {
'/specification': 'specification',
'/deployment': 'deployment',
'/migration': 'migration',
'/threat-model': 'threat-model',
'/ai-access': 'ai-access',
'/faq': 'faq',
'/resumen': 'resumen', // Spanish summary (bonus)
'/getting-started': 'getting-started',
'/schemas': 'schemas',
'/test-vectors': 'test-vectors',
'/reference-impl': 'reference-impl',
'/aap-vs-web-bot-auth': 'aap-vs-web-bot-auth',
};
export function proxy(request: NextRequest) {
const { pathname } = request.nextUrl;
// Handle direct .md URLs (e.g., /specification.md)
if (pathname.endsWith('.md')) {
const baseRoute = pathname.replace(/\.md$/, '');
const slug = MARKDOWN_ROUTES[baseRoute];
if (slug) {
// Rewrite to API route that serves markdown
const apiUrl = new URL(`/api/markdown/${slug}`, request.url);
return NextResponse.rewrite(apiUrl);
}
}
// Check if this route has a markdown version
const slug = MARKDOWN_ROUTES[pathname];
if (!slug) {
return NextResponse.next();
}
// Check if client accepts markdown via Accept header
const acceptHeader = request.headers.get('accept') || '';
const acceptsMarkdown = acceptHeader.includes('text/markdown') ||
acceptHeader.includes('text/x-markdown');
if (acceptsMarkdown) {
// Rewrite to API route that serves markdown
const apiUrl = new URL(`/api/markdown/${slug}`, request.url);
const response = NextResponse.rewrite(apiUrl);
// Add Vary header for proper caching
response.headers.set('Vary', 'Accept');
// Add Link header pointing to .md URL
response.headers.set(
'Link',
`<${pathname}.md>; rel="alternate"; type="text/markdown"`
);
return response;
}
// For regular browsers, continue with normal HTML response
// but add Link header to advertise markdown availability
const response = NextResponse.next();
response.headers.set(
'Link',
`<${pathname}.md>; rel="alternate"; type="text/markdown"`
);
return response;
}
export const config = {
matcher: [
'/specification',
'/specification.md',
'/deployment',
'/deployment.md',
'/migration',
'/migration.md',
'/threat-model',
'/threat-model.md',
'/ai-access',
'/ai-access.md',
'/faq',
'/faq.md',
'/resumen',
'/resumen.md',
'/getting-started',
'/getting-started.md',
'/schemas',
'/schemas.md',
'/test-vectors',
'/test-vectors.md',
'/reference-impl',
'/reference-impl.md',
'/aap-vs-web-bot-auth',
'/aap-vs-web-bot-auth.md',
],
};