~repos /website
git clone https://pyrossh.dev/repos/website.git
木 Personal website of pyrossh. Built with astrojs, shiki, vite.
39bbb499
—
pyrossh 1 year ago
imporve perf
- routes/_middleware.ts +19 -14
routes/_middleware.ts
CHANGED
|
@@ -9,22 +9,27 @@ const ipInfoClient = new IPinfoWrapper("3f197e26fa9210");
|
|
|
9
9
|
export async function handler(req: Request, ctx: FreshContext) {
|
|
10
10
|
const resp = await ctx.next();
|
|
11
11
|
const userAgent = req.headers.get("user-agent") || "";
|
|
12
|
-
const ipRes = await ipInfoClient.lookupIp(ctx.remoteAddr.hostname);
|
|
13
12
|
if (
|
|
14
|
-
req.headers.get("accept")?.includes("text/html") &&
|
|
13
|
+
req.headers.get("accept")?.includes("text/html") && !isbot(userAgent)
|
|
15
|
-
!isbot(userAgent) &&
|
|
16
|
-
!ipRes.bogon
|
|
17
14
|
) {
|
|
15
|
+
ipInfoClient.lookupIp(ctx.remoteAddr.hostname)
|
|
16
|
+
.then((ipRes: { bogon: boolean; country: string }) => {
|
|
17
|
+
if (!ipRes.bogon) {
|
|
18
|
-
|
|
18
|
+
const pathname = new URL(req.url).pathname;
|
|
19
|
-
|
|
19
|
+
const referer = req.headers.get("referer") || "";
|
|
20
|
-
|
|
20
|
+
const parser = new UAParser(userAgent);
|
|
21
|
-
|
|
21
|
+
recordVisit(
|
|
22
|
-
|
|
22
|
+
pathname,
|
|
23
|
-
|
|
23
|
+
referer,
|
|
24
|
-
|
|
24
|
+
ipRes.country,
|
|
25
|
-
|
|
25
|
+
parser.getOS().name,
|
|
26
|
-
|
|
26
|
+
parser.getBrowser().name,
|
|
27
|
-
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
})
|
|
30
|
+
.catch((err: Error) => {
|
|
31
|
+
console.log("failed to get ip", err);
|
|
32
|
+
});
|
|
28
33
|
}
|
|
29
34
|
return resp;
|
|
30
35
|
}
|