-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
120 lines (106 loc) · 3.44 KB
/
middleware.ts
File metadata and controls
120 lines (106 loc) · 3.44 KB
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
import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";
import { createServerClient } from "@supabase/ssr";
/**
* Middleware for Replay:
* 1. Handle Supabase auth session refresh
* 2. Route "/" to landing page and "/tool" to the app
*/
// AI crawler user-agent patterns — always allow through without auth overhead
const AI_CRAWLER_PATTERNS = [
"GPTBot", "ChatGPT-User", "ClaudeBot", "PerplexityBot",
"GoogleOther", "Google-Extended", "Amazonbot", "Bytespider",
"CCBot", "anthropic-ai", "cohere-ai",
];
export async function middleware(req: NextRequest) {
const { pathname } = req.nextUrl;
let res = NextResponse.next({
request: {
headers: req.headers,
},
});
// Skip static files and API routes
if (
pathname.startsWith("/_next") ||
pathname.startsWith("/api") ||
pathname.includes(".")
) {
return res;
}
// Allow AI crawlers through immediately — no auth overhead, no redirects
const ua = req.headers.get("user-agent") || "";
if (AI_CRAWLER_PATTERNS.some(bot => ua.includes(bot))) {
return res;
}
// Only create Supabase client if env vars are set
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL;
const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY;
if (supabaseUrl && supabaseAnonKey) {
try {
// Create Supabase client for session management
const supabase = createServerClient(supabaseUrl, supabaseAnonKey, {
cookies: {
get(name: string) {
try {
return req.cookies.get(name)?.value;
} catch {
return undefined;
}
},
set(name: string, value: string, options: any) {
try {
req.cookies.set({ name, value, ...options });
res = NextResponse.next({
request: { headers: req.headers },
});
res.cookies.set({ name, value, ...options });
} catch {
// Ignore cookie set errors
}
},
remove(name: string, options: any) {
try {
req.cookies.set({ name, value: "", ...options });
res = NextResponse.next({
request: { headers: req.headers },
});
res.cookies.set({ name, value: "", ...options });
} catch {
// Ignore cookie remove errors
}
},
},
});
// Refresh session if needed (important for server components)
await supabase.auth.getSession();
} catch {
// If Supabase auth fails (e.g., invalid UTF-8 in cookies), continue without auth
console.error("Middleware auth error - continuing without session");
}
}
// Routing: Home -> Landing
if (pathname === "/") {
const url = req.nextUrl.clone();
url.pathname = "/landing";
return NextResponse.rewrite(url, { headers: res.headers });
}
// Routing: /tool -> App (page.tsx at root)
if (pathname === "/tool" || pathname.startsWith("/tool/")) {
const url = req.nextUrl.clone();
url.pathname = "/";
return NextResponse.rewrite(url, { headers: res.headers });
}
return res;
}
export const config = {
matcher: [
/*
* Match all paths except:
* - _next/static, _next/image
* - favicon.ico
* - public files with extensions
* - API routes (handled separately)
*/
"/((?!_next/static|_next/image|favicon.ico|.*\\..*|api).*)",
],
};