I want to reduce my overhead on function calls, fix cold start and also don't want to load extra stuff that is not needed in specific function call scope.
Like I have multiple functions, even scheduler functions too. So const {onSchedule} = require("firebase-functions/v2/scheduler");
is not required for every function, but will load if I do at the start of the file, instead of each function's if block.
// index.ts
export const httpFn = functions.https.onRequest(async (request, response) => {
await (await import('./fn/httpFn')).myFun(request, response)
})
According to this article, the above sample code can do just one import, while in my case I have multiple imports respective to the function.
So based on this answer and comment, I wrote my code something like this (as I have a few more functions, but for sample code here is my full index.js
file)
"use strict";
// Reference: https://cloud.google.com/functions/docs/configuring/env-var
// to save the cold start (removing the overhead by polluting with extra load)
const calledFunctionName = process.env.FUNCTION_TARGET ||
process.env.FUNCTION_NAME ||
process.env.K_SERVICE;
// The Firebase Admin SDK
const {initializeApp} = require("firebase-admin/app");
if (calledFunctionName && calledFunctionName === "addCourseData") {
const {addCourseData} = require("./my-functions/my-course-functions");
const {onRequest, HttpsError} = require("firebase-functions/v2/https");
const {getFirestore} = require("firebase-admin/firestore");
initializeApp();
const firestoreDB = getFirestore();
// Take the email and adds an entry to Firestore with Course data
exports.addCourseData = onRequest(async (request, response) => {
return await addCourseData(HttpsError,
firestoreDB, request, response);
});
}
But somehow, when I deploy using command firebase --only functions:addCourseData deploy
, I see Deploy complete!
but can't see the function under the Functions in the Firebase console, so can't access it from the client side too.
So it seems, I'm missing something or making a mistake while implementing that approach. Kindly help!
NOTE: Before using this approach, the function addCourseData
was working properly and tested many times from the client side. And for this approach, I only modified index.js
, not the my-course-functions.js