-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
64 lines (59 loc) · 1.98 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
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
// Define your redirects in a map for easy maintenance
const REDIRECTS = {
"/chat": {
destination:
"https://calendar.google.com/calendar/appointments/schedules/AcZssZ3biRg4mJVZ-7Su6oo1WwGuBbpPbhluqJ5COwrICua5MuisV61_yWilOEcRWCkZcnFNSo1JzWA6?gv=true",
eventName: "calendar_redirect",
},
"/x": {
destination: "https://x.com/connorwforsyth",
eventName: "x_redirect",
},
"/linkedin": {
destination: "https://linkedin.com/in/connorwforsyth",
eventName: "linkedin_redirect",
},
"/cv": {
destination: "https://cv.connorforsyth.co",
eventName: "cv_redirect",
},
"/github": {
destination: "https://github.com/connorwforsyth",
eventName: "github_redirect",
},
// Add more redirects as needed
};
export async function middleware(request: NextRequest) {
const path = request.nextUrl.pathname;
const redirect = REDIRECTS[path as keyof typeof REDIRECTS];
if (redirect) {
// Send the analytics event using a POST request
fetch("https://app.posthog.com/capture/", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.NEXT_PUBLIC_POSTHOG_KEY}`,
},
body: JSON.stringify({
api_key: process.env.NEXT_PUBLIC_POSTHOG_KEY,
event: redirect.eventName,
distinct_id:
request.cookies.get("ph_distinct_id")?.value || "unknown_user",
properties: {
$current_url: request.url,
$pathname: path,
destination: redirect.destination,
referrer: request.headers.get("referer"),
userAgent: request.headers.get("user-agent"),
},
}),
}).catch(console.error); // Handle any errors silently to ensure redirect still works
return NextResponse.redirect(redirect.destination);
}
}
// Update the matcher to include all redirect paths
export const config = {
matcher: Object.keys(REDIRECTS),
};