Skip to content

Commit 5a0d3a4

Browse files
committed
put middleware back but run it on node
1 parent 8edb087 commit 5a0d3a4

File tree

1 file changed

+47
-46
lines changed

1 file changed

+47
-46
lines changed

apps/web/middleware.ts

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// import { db } from "@cap/database";
1+
import { db } from "@cap/database";
22
import { organizations } from "@cap/database/schema";
33
import { buildEnv, serverEnv } from "@cap/env";
44
import { eq } from "drizzle-orm";
@@ -66,58 +66,59 @@ export async function middleware(request: NextRequest) {
6666
try {
6767
// We're on a custom domain at this point
6868
// Only allow /s/ routes for custom domains
69-
// if (!path.startsWith("/s/")) {
70-
const url = new URL(request.url);
71-
url.hostname = webUrl;
72-
return NextResponse.redirect(url);
73-
// }
74-
75-
// // Check if we have a cached verification
76-
// const verifiedDomain = request.cookies.get("verified_domain");
77-
// if (verifiedDomain?.value === hostname) return NextResponse.next();
78-
79-
// // Query the space with this custom domain
80-
// const [organization] = await db()
81-
// .select()
82-
// .from(organizations)
83-
// .where(eq(organizations.customDomain, hostname));
84-
85-
// if (!organization || !organization.domainVerified) {
86-
// // If no verified custom domain found, redirect to main domain
87-
// const url = new URL(request.url);
88-
// url.hostname = webUrl;
89-
// return NextResponse.redirect(url);
90-
// }
91-
92-
// // Set verification cookie for non-API routes too
93-
// const response = NextResponse.next();
94-
// response.cookies.set("verified_domain", hostname, {
95-
// httpOnly: true,
96-
// secure: process.env.NODE_ENV === "production",
97-
// sameSite: "strict",
98-
// maxAge: 3600, // Cache for 1 hour
99-
// });
100-
101-
// // Get the pathname and referrer
102-
// const { pathname } = request.nextUrl;
103-
// const referrer = request.headers.get("referer") || "";
104-
105-
// // Parse user agent with the userAgent utility
106-
// const ua = userAgent(request);
107-
108-
// // Add custom headers to check in generateMetadata
109-
// response.headers.set("x-pathname", pathname);
110-
// response.headers.set("x-referrer", referrer);
111-
// response.headers.set("x-user-agent", JSON.stringify(ua));
112-
113-
// return response;
69+
if (!path.startsWith("/s/")) {
70+
const url = new URL(request.url);
71+
url.hostname = webUrl;
72+
return NextResponse.redirect(url);
73+
}
74+
75+
// Check if we have a cached verification
76+
const verifiedDomain = request.cookies.get("verified_domain");
77+
if (verifiedDomain?.value === hostname) return NextResponse.next();
78+
79+
// Query the space with this custom domain
80+
const [organization] = await db()
81+
.select()
82+
.from(organizations)
83+
.where(eq(organizations.customDomain, hostname));
84+
85+
if (!organization || !organization.domainVerified) {
86+
// If no verified custom domain found, redirect to main domain
87+
const url = new URL(request.url);
88+
url.hostname = webUrl;
89+
return NextResponse.redirect(url);
90+
}
91+
92+
// Set verification cookie for non-API routes too
93+
const response = NextResponse.next();
94+
response.cookies.set("verified_domain", hostname, {
95+
httpOnly: true,
96+
secure: process.env.NODE_ENV === "production",
97+
sameSite: "strict",
98+
maxAge: 3600, // Cache for 1 hour
99+
});
100+
101+
// Get the pathname and referrer
102+
const { pathname } = request.nextUrl;
103+
const referrer = request.headers.get("referer") || "";
104+
105+
// Parse user agent with the userAgent utility
106+
const ua = userAgent(request);
107+
108+
// Add custom headers to check in generateMetadata
109+
response.headers.set("x-pathname", pathname);
110+
response.headers.set("x-referrer", referrer);
111+
response.headers.set("x-user-agent", JSON.stringify(ua));
112+
113+
return response;
114114
} catch (error) {
115115
console.error("Error in middleware:", error);
116116
return notFound();
117117
}
118118
}
119119

120120
export const config = {
121+
runtime: "nodejs",
121122
matcher: [
122123
/*
123124
* Match all request paths except for the ones starting with:

0 commit comments

Comments
 (0)