forked from supabase/supabase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
36 lines (31 loc) · 907 Bytes
/
middleware.ts
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
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
import isbot from 'isbot'
export function middleware(request: NextRequest) {
const specs = ['javascript', 'dart', 'csharp']
let version = ''
if (request.url.includes('/v1/')) {
version = 'v1'
}
if (request.url.includes('/v0/')) {
version = 'v0'
}
if (isbot(request.headers.get('user-agent'))) {
for (const lib of specs) {
if (request.url.includes(`reference/${lib}`)) {
const requestSlug = request.url.split('/').pop()
return NextResponse.rewrite(
new URL(
`/docs/reference/${lib}/${version ? version + '/' : ''}crawlers/${requestSlug}`,
request.url
).toString()
)
}
}
} else {
return NextResponse.next()
}
}
export const config = {
matcher: '/((?!api|_next|static|public|favicon.ico).*)',
}