Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
refactor(web): replace middleware with proxy module for Next.js 16
Richie McIlroy793f4b2cfdf8
diff --git a/apps/web/middleware.ts b/apps/web/proxy.tssimilarity index 75%rename from apps/web/middleware.tsrename to apps/web/proxy.tsindex 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 /loginif (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 redirectsreturn 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 domainsif (!path.startsWith("/s/")) {const url = new URL(request.url);url.hostname = webUrl;return NextResponse.redirect(url);}- // Check if we have a cached verificationconst verifiedDomain = request.cookies.get("verified_domain");if (verifiedDomain?.value === hostname) return NextResponse.next();- // Query the space with this custom domainconst [organization] = await db().select().from(organizations).where(eq(organizations.customDomain, hostname));if (!organization || !organization.domainVerified) {- // If no verified custom domain found, redirect to main domainconst url = new URL(request.url);url.hostname = webUrl;return NextResponse.redirect(url);}- // Set verification cookie for non-API routes tooconst 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 referrerconst { pathname } = request.nextUrl;const referrer = request.headers.get("referer") || "";- // Parse user agent with the userAgent utilityconst ua = userAgent(request);- // Add custom headers to check in generateMetadataresponse.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).*)",],};