Skip to content

Commit 69ef228

Browse files
author
Deepak Pandey
committed
Fix Supabase build error by making client creation lazy
- Replace module-level Supabase client creation with lazy initialization - Fix admin-collaboration, admin-judges, admin-mentors, admin-sponsorship, admin-volunteers routes - This resolves the 'supabaseUrl is required' build error during Next.js page data collection - Build now completes successfully without environment variable issues
1 parent a6717cc commit 69ef228

5 files changed

Lines changed: 55 additions & 25 deletions

File tree

app/api/admin-collaboration/route.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import { NextResponse } from "next/server";
22
import { createClient } from "@supabase/supabase-js";
33

4-
// Setup Supabase client with service role key (bypasses RLS)
5-
const supabase = createClient(
6-
process.env.NEXT_PUBLIC_SUPABASE_URL!,
7-
process.env.SUPABASE_SERVICE_ROLE_KEY!
8-
);
4+
// Create Supabase client function to avoid build-time initialization
5+
function getSupabaseClient() {
6+
return createClient(
7+
process.env.NEXT_PUBLIC_SUPABASE_URL!,
8+
process.env.SUPABASE_SERVICE_ROLE_KEY!
9+
);
10+
}
911

1012
// GET: List all collaboration applications
1113
export async function GET() {
14+
const supabase = getSupabaseClient();
1215
const { data, error } = await supabase
1316
.from("collaboration_applications")
1417
.select("*")
@@ -23,6 +26,7 @@ export async function GET() {
2326
// POST: Create a new collaboration application
2427
export async function POST(req: Request) {
2528
const body = await req.json();
29+
const supabase = getSupabaseClient();
2630
const { data, error } = await supabase
2731
.from("collaboration_applications")
2832
.insert([body])
@@ -41,6 +45,7 @@ export async function PATCH(req: Request) {
4145
if (!id) {
4246
return NextResponse.json({ error: "Missing id" }, { status: 400 });
4347
}
48+
const supabase = getSupabaseClient();
4449
const { data, error } = await supabase
4550
.from("collaboration_applications")
4651
.update(fields)
@@ -60,6 +65,7 @@ export async function DELETE(req: Request) {
6065
if (!id) {
6166
return NextResponse.json({ error: "Missing id" }, { status: 400 });
6267
}
68+
const supabase = getSupabaseClient();
6369
const { error } = await supabase
6470
.from("collaboration_applications")
6571
.delete()

app/api/admin-judges/route.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import { NextResponse } from "next/server";
22
import { createClient } from "@supabase/supabase-js";
33

4-
// Setup Supabase client with service role key (bypasses RLS)
5-
const supabase = createClient(
6-
process.env.NEXT_PUBLIC_SUPABASE_URL!,
7-
process.env.SUPABASE_SERVICE_ROLE_KEY!
8-
);
4+
// Create Supabase client function to avoid build-time initialization
5+
function getSupabaseClient() {
6+
return createClient(
7+
process.env.NEXT_PUBLIC_SUPABASE_URL!,
8+
process.env.SUPABASE_SERVICE_ROLE_KEY!
9+
);
10+
}
911

1012
// GET: List all judges applications
1113
export async function GET() {
14+
const supabase = getSupabaseClient();
1215
const { data, error } = await supabase
1316
.from("judges_applications")
1417
.select("*")
@@ -23,6 +26,7 @@ export async function GET() {
2326
// POST: Create a new judge application
2427
export async function POST(req: Request) {
2528
const body = await req.json();
29+
const supabase = getSupabaseClient();
2630
const { data, error } = await supabase
2731
.from("judges_applications")
2832
.insert([body])
@@ -41,6 +45,7 @@ export async function PATCH(req: Request) {
4145
if (!id) {
4246
return NextResponse.json({ error: "Missing id" }, { status: 400 });
4347
}
48+
const supabase = getSupabaseClient();
4449
const { data, error } = await supabase
4550
.from("judges_applications")
4651
.update(fields)
@@ -60,6 +65,7 @@ export async function DELETE(req: Request) {
6065
if (!id) {
6166
return NextResponse.json({ error: "Missing id" }, { status: 400 });
6267
}
68+
const supabase = getSupabaseClient();
6369
const { error } = await supabase
6470
.from("judges_applications")
6571
.delete()

app/api/admin-mentors/route.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import { NextResponse } from "next/server";
22
import { createClient } from "@supabase/supabase-js";
33

4-
// Setup Supabase client with service role key (bypasses RLS)
5-
const supabase = createClient(
6-
process.env.NEXT_PUBLIC_SUPABASE_URL!,
7-
process.env.SUPABASE_SERVICE_ROLE_KEY!
8-
);
4+
// Create Supabase client function to avoid build-time initialization
5+
function getSupabaseClient() {
6+
return createClient(
7+
process.env.NEXT_PUBLIC_SUPABASE_URL!,
8+
process.env.SUPABASE_SERVICE_ROLE_KEY!
9+
);
10+
}
911

1012
// GET: List all mentor applications
1113
export async function GET() {
14+
const supabase = getSupabaseClient();
1215
const { data, error } = await supabase
1316
.from("mentor_applications")
1417
.select("*")
@@ -23,6 +26,7 @@ export async function GET() {
2326
// POST: Create a new mentor application
2427
export async function POST(req: Request) {
2528
const body = await req.json();
29+
const supabase = getSupabaseClient();
2630
const { data, error } = await supabase
2731
.from("mentor_applications")
2832
.insert([body])
@@ -41,6 +45,7 @@ export async function PATCH(req: Request) {
4145
if (!id) {
4246
return NextResponse.json({ error: "Missing id" }, { status: 400 });
4347
}
48+
const supabase = getSupabaseClient();
4449
const { data, error } = await supabase
4550
.from("mentor_applications")
4651
.update(fields)
@@ -60,6 +65,7 @@ export async function DELETE(req: Request) {
6065
if (!id) {
6166
return NextResponse.json({ error: "Missing id" }, { status: 400 });
6267
}
68+
const supabase = getSupabaseClient();
6369
const { error } = await supabase
6470
.from("mentor_applications")
6571
.delete()

app/api/admin-sponsorship/route.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import { NextResponse } from "next/server";
22
import { createClient } from "@supabase/supabase-js";
33

4-
// Setup Supabase client with service role key (bypasses RLS)
5-
const supabase = createClient(
6-
process.env.NEXT_PUBLIC_SUPABASE_URL!,
7-
process.env.SUPABASE_SERVICE_ROLE_KEY!
8-
);
4+
// Create Supabase client function to avoid build-time initialization
5+
function getSupabaseClient() {
6+
return createClient(
7+
process.env.NEXT_PUBLIC_SUPABASE_URL!,
8+
process.env.SUPABASE_SERVICE_ROLE_KEY!
9+
);
10+
}
911

1012
// GET: List all sponsorship applications
1113
export async function GET() {
14+
const supabase = getSupabaseClient();
1215
const { data, error } = await supabase
1316
.from("sponsorship_applications")
1417
.select("*")
@@ -23,6 +26,7 @@ export async function GET() {
2326
// POST: Create a new sponsorship application
2427
export async function POST(req: Request) {
2528
const body = await req.json();
29+
const supabase = getSupabaseClient();
2630
const { data, error } = await supabase
2731
.from("sponsorship_applications")
2832
.insert([body])
@@ -41,6 +45,7 @@ export async function PATCH(req: Request) {
4145
if (!id) {
4246
return NextResponse.json({ error: "Missing id" }, { status: 400 });
4347
}
48+
const supabase = getSupabaseClient();
4449
const { data, error } = await supabase
4550
.from("sponsorship_applications")
4651
.update(fields)
@@ -60,6 +65,7 @@ export async function DELETE(req: Request) {
6065
if (!id) {
6166
return NextResponse.json({ error: "Missing id" }, { status: 400 });
6267
}
68+
const supabase = getSupabaseClient();
6369
const { error } = await supabase
6470
.from("sponsorship_applications")
6571
.delete()

app/api/admin-volunteers/route.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import { NextResponse } from "next/server";
22
import { createClient } from "@supabase/supabase-js";
33

4-
// Setup Supabase client with service role key (bypasses RLS)
5-
const supabase = createClient(
6-
process.env.NEXT_PUBLIC_SUPABASE_URL!,
7-
process.env.SUPABASE_SERVICE_ROLE_KEY!
8-
);
4+
// Create Supabase client function to avoid build-time initialization
5+
function getSupabaseClient() {
6+
return createClient(
7+
process.env.NEXT_PUBLIC_SUPABASE_URL!,
8+
process.env.SUPABASE_SERVICE_ROLE_KEY!
9+
);
10+
}
911

1012
// GET: List all volunteer applications
1113
export async function GET() {
14+
const supabase = getSupabaseClient();
1215
const { data, error } = await supabase
1316
.from("volunteer_applications")
1417
.select("*")
@@ -23,6 +26,7 @@ export async function GET() {
2326
// POST: Create a new volunteer application
2427
export async function POST(req: Request) {
2528
const body = await req.json();
29+
const supabase = getSupabaseClient();
2630
const { data, error } = await supabase
2731
.from("volunteer_applications")
2832
.insert([body])
@@ -41,6 +45,7 @@ export async function PATCH(req: Request) {
4145
if (!id) {
4246
return NextResponse.json({ error: "Missing id" }, { status: 400 });
4347
}
48+
const supabase = getSupabaseClient();
4449
const { data, error } = await supabase
4550
.from("volunteer_applications")
4651
.update(fields)
@@ -60,6 +65,7 @@ export async function DELETE(req: Request) {
6065
if (!id) {
6166
return NextResponse.json({ error: "Missing id" }, { status: 400 });
6267
}
68+
const supabase = getSupabaseClient();
6369
const { error } = await supabase
6470
.from("volunteer_applications")
6571
.delete()

0 commit comments

Comments
 (0)