I have developed an API in Next.js 14 where I use Firebase Admin to fetch values from Firestore. When running locally, the API retrieves fresh data as expected. However, after deploying to Vercel, the API always returns the same values and does not seem to fetch the latest data from Firestore. It behaves as if the response is static.
Here is a simplified version of my API code:
'use server';
import { admin } from '@/lib/firebase/firebase-admin-config';
// GET ALL REQUESTS
type GetResponseType = {
...
};
export async function GET(req: Request) {
try {
const snapshot = await admin
.firestore()
.collection('panelNewSalePrice')
.get();
if (snapshot.empty) {
return Response.json({ data: [] }, { status: 200 });
}
let dataRes: GetResponseType[] = [];
for (const doc of snapshot.docs) {
const data = doc.data();
...
dataRes.push({
...
});
}
return Response.json(
{ data: dataRes },
{
status: 200,
headers: {
'Cache-Control':
'no-store, no-cache, must-revalidate, proxy-revalidate',
Pragma: 'no-cache',
Expires: '0',
},
}
);
} catch (error) {
return Response.json('', { status: 500 });
}
}
import * as admin from 'firebase-admin';
if (admin.apps.length === 0) {
admin.initializeApp({
credential: admin.credential.cert({
projectId: process.env.FB_PROJECT_ID,
clientEmail: process.env.FB_CLIENT_EMAIL,
privateKey: process.env.FB_PRIVATE_KEY?.replace(/\n/g, 'n'),
}),
});
}
export { admin };
const res = await fetch(
`${process.env.NEXT_PUBLIC_BASE_URL}/api/v1/profit-margin`,
{
method: 'GET',
headers: {
'Cache-Control': 'no-cache',
},
cache: 'no-store',
next: { revalidate: 1 },
}
);
I’ve tried disabled caching by setting appropriate headers in the response.
Has anyone faced a similar issue or can provide insights on what might be causing this behavior? Any help would be greatly appreciated!