-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
43 lines (39 loc) · 1.07 KB
/
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
37
38
39
40
41
42
43
import { type NextRequest, NextResponse } from 'next/server';
import { isUserBot } from '@/lib/bot';
export const middleware = async (request: NextRequest) => {
if (process.env.ALLOWED_BOTS) {
const { isBot, userAgent } = isUserBot(request.headers);
if (isBot) {
const isAllowedBot =
userAgent &&
(() => {
try {
const allowedBots = JSON.parse(process.env.ALLOWED_BOTS || '[]');
return (
Array.isArray(allowedBots) &&
allowedBots.some((bot) =>
userAgent.toLowerCase().includes(bot.toLowerCase())
)
);
} catch (error) {
return false;
}
})();
if (!isAllowedBot) {
return new Response('Forbidden', { status: 403 });
}
}
}
return NextResponse.next();
};
export const config = {
matcher: [
{
source: '/lookup/:domain*',
missing: [
{ type: 'header', key: 'next-router-prefetch' },
{ type: 'header', key: 'purpose', value: 'prefetch' },
],
},
],
};