-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
42 lines (33 loc) · 1.14 KB
/
proxy.ts
File metadata and controls
42 lines (33 loc) · 1.14 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
import { type NextRequest, NextResponse } from 'next/server'
const BLOG_POST_REGEX = /^\/blog\/([^/]+)$/
const IS_VERCEL_PREVIEW = process.env.VERCEL_ENV === 'preview'
const applyPreviewNoindex = (response: NextResponse): NextResponse => {
if (IS_VERCEL_PREVIEW) {
response.headers.set('X-Robots-Tag', 'noindex, nofollow')
}
return response
}
export const proxy = (request: NextRequest) => {
const { pathname } = request.nextUrl
const accept = request.headers.get('accept') || ''
if (pathname.startsWith('/ingest')) {
return NextResponse.next()
}
// Content negotiation for blog posts: serve markdown when requested
const match = pathname.match(BLOG_POST_REGEX)
if (match) {
const slug = match[1]
const prefersMarkdown =
accept.includes('text/markdown') ||
(accept.includes('text/plain') && !accept.includes('text/html'))
if (prefersMarkdown) {
return applyPreviewNoindex(
NextResponse.rewrite(new URL(`/api/blog/${slug}/raw`, request.url)),
)
}
}
return applyPreviewNoindex(NextResponse.next())
}
export const config = {
matcher: ['/((?!_next/static|_next/image|ingest).*)'],
}