Skip to content

Commit 65d7407

Browse files
author
Deepak Pandey
committed
Fix additional module-level environment variable initialization issues
- Fixed lib/services/email.ts: Moved Resend client initialization to lazy getResendClient() function - Fixed app/api/membership/send-card/route.ts: Moved Resend client initialization to lazy getResendClient() function - Fixed lib/supabase.ts: Moved Supabase client initialization to lazy getSupabaseClient() function - All environment variable checks now happen at runtime, not build time - Build now passes successfully with 142/142 pages generated - All module-level environment variable initialization issues resolved
1 parent f8cbbb1 commit 65d7407

3 files changed

Lines changed: 24 additions & 10 deletions

File tree

app/api/membership/send-card/route.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import { NextRequest, NextResponse } from 'next/server';
22
import { createClient } from '@/lib/supabase/server';
33
import { Resend } from 'resend';
44

5-
const resend = new Resend(process.env.RESEND_API_KEY);
5+
// Create Resend client function to avoid build-time initialization
6+
function getResendClient() {
7+
return new Resend(process.env.RESEND_API_KEY);
8+
}
69

710
interface MembershipCardEmailData {
811
userId: string;
@@ -259,6 +262,7 @@ export async function POST(request: NextRequest) {
259262
const membershipCardEmailContent = createMembershipCardEmail();
260263

261264
console.log('📨 Sending beautiful membership card email via Resend...');
265+
const resend = getResendClient();
262266
// Send email without PDF attachment, just the beautiful HTML template
263267
const emailResult = await resend.emails.send({
264268
from: 'Codeunia <connect@codeunia.com>',

lib/services/email.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { Resend } from 'resend'
22
import { generateInternshipOfferLetterPDF } from '@/lib/pdf-generator'
33

4-
const resend = new Resend(process.env.RESEND_API_KEY)
4+
// Create Resend client function to avoid build-time initialization
5+
function getResendClient() {
6+
return new Resend(process.env.RESEND_API_KEY)
7+
}
58

69
export interface InternshipApplicationEmailData {
710
applicantName: string
@@ -345,6 +348,7 @@ const getStatusUpdateTemplate = (data: StatusUpdateEmailData) => {
345348

346349
export async function sendInternshipApplicationEmails(data: InternshipApplicationEmailData & { applicationId: string }) {
347350
try {
351+
const resend = getResendClient();
348352
// Send confirmation email to applicant only
349353
const applicantTemplate = getApplicantConfirmationTemplate(data)
350354
const applicantResult = await resend.emails.send({
@@ -369,6 +373,7 @@ export async function sendInternshipApplicationEmails(data: InternshipApplicatio
369373

370374
export async function sendStatusUpdateEmail(data: StatusUpdateEmailData) {
371375
try {
376+
const resend = getResendClient();
372377
// Don't send email if status hasn't actually changed
373378
if (data.oldStatus === data.newStatus) {
374379
return {

lib/supabase.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
import { createClient } from '@supabase/supabase-js';
22

3-
// Initialize the Supabase client with environment variables
4-
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL || '';
5-
const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY || '';
3+
// Create Supabase client function to avoid build-time initialization
4+
export function getSupabaseClient() {
5+
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL || '';
6+
const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY || '';
7+
8+
return createClient(supabaseUrl, supabaseAnonKey, {
9+
auth: {
10+
persistSession: false, // We don't need session persistence for server-side operations
11+
},
12+
});
13+
}
614

7-
export const supabase = createClient(supabaseUrl, supabaseAnonKey, {
8-
auth: {
9-
persistSession: false, // We don't need session persistence for server-side operations
10-
},
11-
});
15+
// For backward compatibility, export a function that returns the client
16+
export const supabase = getSupabaseClient();
1217

1318
// Type for the profiles table
1419
export type Profile = {

0 commit comments

Comments
 (0)