-
Notifications
You must be signed in to change notification settings - Fork 3
/
middleware.ts
42 lines (38 loc) · 1.21 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
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
function parseUrl(url: string) {
const urlSplit = url.split("/");
const username = urlSplit[3];
const id = urlSplit[5];
return { username, id };
}
// Originally written by Steven Tey for dub.sh
// https://github.com/steven-tey/dub/blob/3112be1e8b8ce5eea09b1fb484afe142a1d9b6ae/lib/middleware/utils.ts#L37C1-L54C3
const detectBot = (req: NextRequest) => {
const url = req.nextUrl;
if (url.searchParams.get("bot")) return true;
const ua = req.headers.get("User-Agent");
if (ua) {
return /bot|chatgpt|facebookexternalhit|WhatsApp|google|baidu|bing|msn|duckduckbot|teoma|slurp|yandex|MetaInspector|node-fetch|axios|got/i.test(
ua
);
}
return false;
};
export function middleware(req: NextRequest) {
const { username, id } = parseUrl(req.url);
const isBot = detectBot(req);
if (isBot) {
return NextResponse.rewrite(
new URL(`/og/${username}/status/${id}`, req.url)
);
} else {
return NextResponse.redirect(
new URL(`/${username}/status/${id}`, "https://twitter.com")
);
}
}
export const config = {
// matches [username]/status/[id]
matcher: ["/([^/]+)/status/([^/]+)/"],
};