Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

refactor(web): replace middleware with proxy module for Next.js 16

Richie McIlroy793f4b2cfdf8
diff --git a/apps/web/middleware.ts b/apps/web/proxy.ts
similarity index 75%
rename from apps/web/middleware.ts
rename to apps/web/proxy.ts
index ff30a4bb7..ec57ff223 100644
--- a/apps/web/middleware.ts
+++ b/apps/web/proxy.ts
@@ -20,11 +20,10 @@ const mainOrigins = [
addHttps(serverEnv().VERCEL_PROJECT_PRODUCTION_URL_HOST),
].filter(Boolean) as string[];
-export async function middleware(request: NextRequest) {
+export async function proxy(request: NextRequest) {
const url = new URL(request.url);
const path = url.pathname;
- // Add anti-clickjacking headers for /login
if (path.startsWith("/login")) {
const response = NextResponse.next();
response.headers.set("X-Frame-Options", "SAMEORIGIN");
@@ -59,76 +58,58 @@ export async function middleware(request: NextRequest) {
}
if (mainOrigins.some((d) => url.origin.startsWith(d))) {
- // We just let the request go through for main domains, page-level logic will handle redirects
return NextResponse.next();
}
const webUrl = new URL(serverEnv().WEB_URL).hostname;
try {
- // We're on a custom domain at this point
- // Only allow /s/ routes for custom domains
if (!path.startsWith("/s/")) {
const url = new URL(request.url);
url.hostname = webUrl;
return NextResponse.redirect(url);
}
- // Check if we have a cached verification
const verifiedDomain = request.cookies.get("verified_domain");
if (verifiedDomain?.value === hostname) return NextResponse.next();
- // Query the space with this custom domain
const [organization] = await db()
.select()
.from(organizations)
.where(eq(organizations.customDomain, hostname));
if (!organization || !organization.domainVerified) {
- // If no verified custom domain found, redirect to main domain
const url = new URL(request.url);
url.hostname = webUrl;
return NextResponse.redirect(url);
}
- // Set verification cookie for non-API routes too
const response = NextResponse.next();
response.cookies.set("verified_domain", hostname, {
httpOnly: true,
secure: process.env.NODE_ENV === "production",
sameSite: "strict",
- maxAge: 3600, // Cache for 1 hour
+ maxAge: 3600,
});
- // Get the pathname and referrer
const { pathname } = request.nextUrl;
const referrer = request.headers.get("referer") || "";
- // Parse user agent with the userAgent utility
const ua = userAgent(request);
- // Add custom headers to check in generateMetadata
response.headers.set("x-pathname", pathname);
response.headers.set("x-referrer", referrer);
response.headers.set("x-user-agent", JSON.stringify(ua));
return response;
} catch (error) {
- console.error("Error in middleware:", error);
+ console.error("Error in proxy:", error);
return notFound();
}
}
export const config = {
- runtime: "nodejs",
matcher: [
- /*
- * Match all request paths except for the ones starting with:
- * - api (API routes)
- * - _next/static (static files)
- * - _next/image (image optimization files)
- * - favicon.ico, robots.txt, sitemap.xml (static files)
- */
"/((?!api|_next/static|_next/image|favicon.ico|robots.txt|sitemap.xml).*)",
],
};