diff --git a/apps/web/middleware.ts b/apps/web/middleware.ts index 1d4b84204e..84269aea4a 100644 --- a/apps/web/middleware.ts +++ b/apps/web/middleware.ts @@ -11,6 +11,14 @@ import { csp } from "@lib/csp"; import { abTestMiddlewareFactory } from "./abTest/middlewareFactory"; +const safeGet = async (key: string): Promise => { + try { + return get(key); + } catch (error) { + // Don't crash if EDGE_CONFIG env var is missing + } +}; + const middleware = async (req: NextRequest): Promise> => { const url = req.nextUrl; const requestHeaders = new Headers(req.headers); @@ -24,18 +32,12 @@ const middleware = async (req: NextRequest): Promise> => { // // - For this reason our matchers are sufficient for an app-wide maintenance page. // - try { - // Check whether the maintenance page should be shown - const isInMaintenanceMode = await get("isInMaintenanceMode"); - // If is in maintenance mode, point the url pathname to the maintenance page - if (isInMaintenanceMode) { - req.nextUrl.pathname = `/maintenance`; - return NextResponse.rewrite(req.nextUrl); - } - } catch (error) { - // show the default page if EDGE_CONFIG env var is missing, - // but log the error to the console - // console.error(error); + // Check whether the maintenance page should be shown + const isInMaintenanceMode = await safeGet("isInMaintenanceMode"); + // If is in maintenance mode, point the url pathname to the maintenance page + if (isInMaintenanceMode) { + req.nextUrl.pathname = `/maintenance`; + return NextResponse.rewrite(req.nextUrl); } } @@ -61,14 +63,10 @@ const middleware = async (req: NextRequest): Promise> => { } if (url.pathname.startsWith("/api/auth/signup")) { - try { - const isSignupDisabled = await get("isSignupDisabled"); - // If is in maintenance mode, point the url pathname to the maintenance page - if (isSignupDisabled) { - return NextResponse.json({ error: "Signup is disabled" }, { status: 503 }); - } - } catch (error) { - // Don't crash if EDGE_CONFIG env var is missing + const isSignupDisabled = await safeGet("isSignupDisabled"); + // If is in maintenance mode, point the url pathname to the maintenance page + if (isSignupDisabled) { + return NextResponse.json({ error: "Signup is disabled" }, { status: 503 }); } }