From bbc28514f1e7115c74a9743ec8c47b5b89488e99 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Mon, 18 May 2026 13:26:44 +0000 Subject: [PATCH] perf: optimize island visit query to use relational fetch Replaced two sequential database queries (fetching the user owner ID and then the island by user ID) with a single, optimized inner-join query using Supabase's `!inner` directive. This eliminates an N+1 style inefficiency and resolves the island and user association in a single round trip. Co-authored-by: Ap-0007 <200978133+Ap-0007@users.noreply.github.com> --- app/api/visit/route.ts | 24 +++++------------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/app/api/visit/route.ts b/app/api/visit/route.ts index 5b6a211..af5949b 100644 --- a/app/api/visit/route.ts +++ b/app/api/visit/route.ts @@ -27,30 +27,16 @@ export async function POST(request: NextRequest) { ); } - // Find island owner - const { data: owner } = await supabase - .from("users") - .select("id") - .eq("username", islandUsername) - .single(); - - if (!owner) { - return NextResponse.json( - { error: "Island owner not found" }, - { status: 404 } - ); - } - - // Find island + // Find island by owner's username const { data: island } = await supabase .from("islands") - .select("id") - .eq("user_id", owner.id) + .select("id, user_id, users!inner(username)") + .eq("users.username", islandUsername) .single(); if (!island) { return NextResponse.json( - { error: "Island not found" }, + { error: "Island or owner not found" }, { status: 404 } ); } @@ -67,7 +53,7 @@ export async function POST(request: NextRequest) { } // Don't log visit if visiting own island - if (visitorId === owner.id) { + if (visitorId === island.user_id) { const { count } = await supabase .from("visits") .select("*", { count: "exact", head: true })