-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
122 lines (107 loc) · 3.45 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { NextRequest, NextResponse } from "next/server";
// import Umami from "./utils/useUmami";
// import { redis, getUrl } from "./lib/redis";
export async function middleware(req: NextRequest) {
const path = req.nextUrl.pathname.split("/")[1];
if (["favicon.ico", "api", "", "login", "404"].includes(path)) {
return;
}
const link404 = req.nextUrl;
link404.pathname = `/404`;
// const trackEvent = Umami("/");
// const url = await getUrl(path);
const url = await fetch(
`${process.env.NEXT_PUBLIC_AIRTABLE_URI}/links?filterByFormula=shortname='${path}'`,
{
method: "GET",
headers: {
Authorization: `Bearer ${process.env.NEXT_PUBLIC_AIRTABLE_TOKEN}`,
},
}
).then((res) => {
return res.json();
});
if (url?.records[0]?.fields?.url) {
const airtableBody = {
records: [
{
id: url.records[0].id,
fields: {
visit_count: url.records[0].fields.visit_count + 1,
},
},
],
};
const trackingScript = `
<script async defer data-website-id="f479d332-9df1-4719-abc1-ded2eddc020e" src="https://cloud.umami.is/script.js"></script>
<script>
if (window.umami) {
window.umami.trackEvent('shortlink visit', {url: '${url?.records[0]?.fields?.shortname}'});
}
window.location.href = '${url?.records[0]?.fields?.url}';
</script>`;
const redirectHtml = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="refresh" content="0;url=${url?.records[0]?.fields?.url}">
<script async defer data-website-id="f479d332-9df1-4719-abc1-ded2eddc020e" src="https://cloud.umami.is/script.js"></script>
<script>
window.onload = function() {
if (window.umami) {
window.umami.trackEvent('shortlink visit', {url: '${url?.records[0]?.fields?.shortname}'});
}
};
</script>
</head>
<body>
</body>
</html>
`;
// console.log(airtableBody);
const response = await fetch(
`${process.env.NEXT_PUBLIC_AIRTABLE_URI}/links`,
{
method: "PATCH",
headers: {
Authorization: `Bearer ${process.env.NEXT_PUBLIC_AIRTABLE_TOKEN}`,
"Content-Type": "application/json",
},
body: JSON.stringify(airtableBody),
}
);
// console.log(response);
if (response.ok) {
// const redirectUrl = new URL(
// "/redirect",
// process.env.NEXT_PUBLIC_HOSTNAME
// );
// redirectUrl.searchParams.set(
// "shortUrl",
// url?.records[0]?.fields?.shortname
// );
// redirectUrl.searchParams.set(
// "originalUrl",
// encodeURIComponent(url?.records[0]?.fields?.url)
// );
// return NextResponse.redirect(
// `${process.env.NEXT_PUBLIC_HOSTNAME}/redirect?shortUrl=${url?.records[0]?.fields?.shortname}&originalUrl=${url?.records[0]?.fields?.url}`
// );
return new NextResponse(redirectHtml, {
headers: { "content-type": "text/html" },
});
// console.log(response.status);
// trackEvent(`${url?.records[0]?.fields?.shortname}`, "visit");
// return NextResponse.redirect(url?.records[0]?.fields?.url);
}
} else {
return NextResponse.redirect(link404);
}
}
export const config = {
matcher: [
"/((?!api|_next/static|_next/image|auth|favicon.ico|robots.txt|images|$).*)",
],
// matcher: "/:shortUrl",
};