From bde88270410131c04701e227420fd22a22b85057 Mon Sep 17 00:00:00 2001 From: Emiya Kiritsugu Date: Wed, 6 May 2026 10:42:56 -0300 Subject: [PATCH 1/2] fix(auth): reduce sequential queries in middleware to prevent 504 timeout --- src/utils/supabase/middleware.ts | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/utils/supabase/middleware.ts b/src/utils/supabase/middleware.ts index ea3e754..81e775d 100644 --- a/src/utils/supabase/middleware.ts +++ b/src/utils/supabase/middleware.ts @@ -45,13 +45,15 @@ export const updateSession = async (request: NextRequest) => { } if (user && isProtectedRoute) { + // Single query to fetch role - if null, user is not a 'funcionario' (ALUNO) const { data: funcionarioProfile } = await supabase .from('funcionarios') - .select('id') + .select('role') .eq('id', user.id) .maybeSingle(); const isFuncionario = !!funcionarioProfile; + const userRole = funcionarioProfile?.role; if (isFuncionario && isAlunoRoute) { const dashboardUrl = request.nextUrl.clone(); @@ -68,13 +70,7 @@ export const updateSession = async (request: NextRequest) => { const isFinancialRoute = FINANCIAL_ROUTES.some((r) => pathname.startsWith(r)); if (isFuncionario && isFinancialRoute) { - const { data: roleData, error } = await supabase - .from('funcionarios') - .select('role') - .eq('id', user.id) - .maybeSingle(); - - if (error || roleData?.role !== 'GERENTE') { + if (userRole !== 'GERENTE') { const dashboardUrl = request.nextUrl.clone(); dashboardUrl.pathname = '/dashboard'; return NextResponse.redirect(dashboardUrl); From b0df206acaafd75b8c28402b61b5e0ddacf6347b Mon Sep 17 00:00:00 2001 From: Emiya Kiritsugu Date: Wed, 6 May 2026 10:43:49 -0300 Subject: [PATCH 2/2] docs(sentinel): log incident resolution for Vercel 504 timeout --- docs/process/sentinel-log.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/docs/process/sentinel-log.md b/docs/process/sentinel-log.md index f2987cc..8c00c48 100644 --- a/docs/process/sentinel-log.md +++ b/docs/process/sentinel-log.md @@ -44,3 +44,24 @@ Related: [ADF-PROTOCOL](./ADF-PROTOCOL.md) | [Next.js 15 Build Fix](./../../supe - **Commits**: 8 commits atômicos realizados e enviados. - **Scripts**: Validados e funcionais via `pg` driver e `PrismaClient`. +--- + +## [2026-05-06] Incident: Vercel 504 Gateway Timeout (Middleware) + +**Status**: RESOLVED +**Impact**: High (Site unreachable in production) +**Symptoms**: `MIDDLEWARE_INVOCATION_TIMEOUT` error on Vercel. + +### 🔍 Analysis (RCA) +Latency accumulation. The middleware performed up to 3 sequential Supabase queries (`auth.getUser` -> `funcionario.id` -> `funcionario.role`). The Brasil/USA cross-region latency exceeded the 5-10s Edge Runtime limit. + +### 💡 Key Learning (Filtro B - Regra de Projeto) +**Mandato de Round-trip Mínimo**: Middleware em Next.js 15 deve realizar no máximo UMA query externa se o banco não estiver na mesma região do Edge. +**Ação**: Consolidar checagem de existência e permissão em um único `.select('role')`. + +### 🛡️ Proof of State +- **PR #99**: Criado e enviado. +- **Deploy**: Realizado via Vercel CLI com sucesso. +- **Verification**: `npm run typecheck` e testes de auth passando. + +