I'm facing an issue while trying to make requests to my Firebase Realtime Database from a Vercel-deployed application. The requests are successful in my local development environment, both in development and production modes. However, once deployed on Vercel, I consistently encounter timeouts and Firebase warnings.
Sample Code That's Failing:
Below is a minimal, reproducible example of the serverless function that fails when deployed on Vercel:
import { NextRequest, NextResponse } from 'next/server';
import { adminDb } from '@/lib';
import { env } from '@/lib';
const headers = {
"Access-Control-Allow-Origin": env.CORS_ORIGIN,
"Access-Control-Allow-Methods": "GET, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type, Authorization",
};
export async function OPTIONS(request: NextRequest) {
return new Response(null, {
status: 204,
headers,
});
}
// Other code omitted for brevity
Notable Points:
- Firebase Realtime Database is throwing warnings, but the message is empty.
- Vercel is returning multiple 504 Gateway Timeout errors, indicating that the serverless function is not completing within the 10-second limit.
Logs from Vercel:
Aug 30 12:25:46.97
GET
---
bsb-backend.vercel.app
/api/public/images/slideshow
[2024-08-30T10:25:46.973Z] @firebase/database: FIREBASE WARNING: {}
Aug 30 12:25:46.52
GET
---
bsb-backend.vercel.app
/api/public/images/footer
[2024-08-30T10:25:46.526Z] @firebase/database: FIREBASE WARNING: {}
// More log entries...
Steps Taken So Far:
- Confirmed stable internet connection.
- Checked database size and latency, which shouldn't be causing issues.
- Using Firebase Admin SDK to interact with Firebase services.
Firebase Initialization Code:
Here’s a condensed version of the Firebase initialization code used in the project:
import * as admin from 'firebase-admin';
if (!admin.apps.length) {
admin.initializeApp({
credential: admin.credential.cert({
projectId: process.env.FIREBASE_PROJECT_ID,
clientEmail: process.env.FIREBASE_CLIENT_EMAIL,
privateKey: process.env.FIREBASE_PRIVATE_KEY?.replace(/\\n/g, '\n'),
}),
databaseURL: process.env.FIREBASE_DATABASE_URL,
});
}
// Other Firebase initialization exports...
Situation Overview:
- Deployment Platform: Vercel
- Database: Firebase Realtime Database with correct credentials and configurations.
- SDK: Using Firebase Admin SDK in serverless functions.
Queries:
- Why am I receiving these `FIREBASE WARNING: {}` messages?
- What could be triggering the timeouts on Vercel whereas it operates smoothly locally?
- Any specific optimizations or setups recommended for Firebase or Vercel to avoid such timeouts?
Your insights or suggestions on what might be going wrong would be highly valued. Thank you!