Skip to content

Commit 793f4b2

Browse files
committed
refactor(web): replace middleware with proxy module for Next.js 16
Made-with: Cursor
1 parent f820fc8 commit 793f4b2

1 file changed

Lines changed: 3 additions & 22 deletions

File tree

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@ const mainOrigins = [
2020
addHttps(serverEnv().VERCEL_PROJECT_PRODUCTION_URL_HOST),
2121
].filter(Boolean) as string[];
2222

23-
export async function middleware(request: NextRequest) {
23+
export async function proxy(request: NextRequest) {
2424
const url = new URL(request.url);
2525
const path = url.pathname;
2626

27-
// Add anti-clickjacking headers for /login
2827
if (path.startsWith("/login")) {
2928
const response = NextResponse.next();
3029
response.headers.set("X-Frame-Options", "SAMEORIGIN");
@@ -59,76 +58,58 @@ export async function middleware(request: NextRequest) {
5958
}
6059

6160
if (mainOrigins.some((d) => url.origin.startsWith(d))) {
62-
// We just let the request go through for main domains, page-level logic will handle redirects
6361
return NextResponse.next();
6462
}
6563

6664
const webUrl = new URL(serverEnv().WEB_URL).hostname;
6765

6866
try {
69-
// We're on a custom domain at this point
70-
// Only allow /s/ routes for custom domains
7167
if (!path.startsWith("/s/")) {
7268
const url = new URL(request.url);
7369
url.hostname = webUrl;
7470
return NextResponse.redirect(url);
7571
}
7672

77-
// Check if we have a cached verification
7873
const verifiedDomain = request.cookies.get("verified_domain");
7974
if (verifiedDomain?.value === hostname) return NextResponse.next();
8075

81-
// Query the space with this custom domain
8276
const [organization] = await db()
8377
.select()
8478
.from(organizations)
8579
.where(eq(organizations.customDomain, hostname));
8680

8781
if (!organization || !organization.domainVerified) {
88-
// If no verified custom domain found, redirect to main domain
8982
const url = new URL(request.url);
9083
url.hostname = webUrl;
9184
return NextResponse.redirect(url);
9285
}
9386

94-
// Set verification cookie for non-API routes too
9587
const response = NextResponse.next();
9688
response.cookies.set("verified_domain", hostname, {
9789
httpOnly: true,
9890
secure: process.env.NODE_ENV === "production",
9991
sameSite: "strict",
100-
maxAge: 3600, // Cache for 1 hour
92+
maxAge: 3600,
10193
});
10294

103-
// Get the pathname and referrer
10495
const { pathname } = request.nextUrl;
10596
const referrer = request.headers.get("referer") || "";
10697

107-
// Parse user agent with the userAgent utility
10898
const ua = userAgent(request);
10999

110-
// Add custom headers to check in generateMetadata
111100
response.headers.set("x-pathname", pathname);
112101
response.headers.set("x-referrer", referrer);
113102
response.headers.set("x-user-agent", JSON.stringify(ua));
114103

115104
return response;
116105
} catch (error) {
117-
console.error("Error in middleware:", error);
106+
console.error("Error in proxy:", error);
118107
return notFound();
119108
}
120109
}
121110

122111
export const config = {
123-
runtime: "nodejs",
124112
matcher: [
125-
/*
126-
* Match all request paths except for the ones starting with:
127-
* - api (API routes)
128-
* - _next/static (static files)
129-
* - _next/image (image optimization files)
130-
* - favicon.ico, robots.txt, sitemap.xml (static files)
131-
*/
132113
"/((?!api|_next/static|_next/image|favicon.ico|robots.txt|sitemap.xml).*)",
133114
],
134115
};

0 commit comments

Comments
 (0)