I hit following problem working with firebase functions, I split functionality into different files, few of them have only custom utility logic.
The thing is that with firebase CLI I only can upload separate functions with :
firebase deploy --only functions:someFunction
But in this case the utility file which function someFunction uses does not get uploaded. To refresh logic in separate files I need to redeploy all the functions by executing :
firebase deploy functions
and I hit deployment limit just in few deploys.
Is there any way I can redeploy a separate file to the server?
UPD
By utility file I mean files that contain logic and used in functions. Say we have function someFunction in the index.js that goes like this :
const commons = require('./commons');
exports.someFunction = function() {
common.sayHello();
}
In this case commons is a plain javascript file (commons.js) that has utility functions, other words - utility file.
exports.sayHello = function() {
console.log('hello!');
}
This is exactly the file I would like to redeploy separately.
It's not possible to upload only a single file when when deploying functions. The entire contents of the functions folder (except node_modules) is replaced for every function at every deployment, even if you are deploying just one function at a time.
Related
I am using firebase with react native . Trying to write some firebase cloud functions. For that i have used firebase init . On console it gives a success message but the function folder is not been created in my project .
I have tried several times re initializing the firebase init but no result.
Try a new folder location. Create a new folder by the name of the backend wherever you'd like and 'CD' into it before you initialize it. i.e. users/haseeb/project and see if it then creates it.
Then, move the functions folder to the root level of the project.
Let's say I have an invoice to generate and the invoice contains the company's logo and other images that would be used each time an invoice is generated.
Would I have to pull those images off the storage bucket? Or can I load it into the functions directory and use it from there, similar to how the serviceaccount.json file is loaded into functions?
EDIT: The way I'm thinking of is
functions/
├── index.js
├── image.jpg
And in index.js
const path = require("path");
const pathToImage = path.join(__dirname,'/image.jpg');
The Firebase/Cloud Functions deployment process packs up everything in your functions directory, and deploys it to the runtime for your functions. This means that you can indeed include any static files you need in the Cloud Functions deployment, and then access them from your code.
This is how you also include your service account, or what happens when you split your Cloud Functions code over multiple files that you then include into index.js.
I am using Google Firebase Cloud Functions with TypeScript, and I found out that even though each function is deployed separately, they all share the same bundles and dependencies, even if some functions do not use them nor import them.
In my case, one cloud function uses Redis and others don't. I have 10 functions. All 10 functions actually end up importing redis related code even though they don't import them.
Since all functions share the same entry point, index.js. It currently seems it's impossible to have separate tree-shaken bundles / entry points for each function.
This is very inefficient in terms of bundle size / cold start timing / memory / etc. It also means as I have more and more functions, bundle size will grow for all functions together. It's not scalable.
Is there any way to not share the entry point, index.js, and have completely separate bundles by using bundlers like webpack?
You can create a different local Firebase working area (with firebase init) for each function that should deploy in isolation from the others. You will have to instruct the CLI not to overwrite the other functions on deployment using the --only functions:yourFunctionName to deploy it.
Or, you can deploy function using Cloud tools (gcloud) instead of Firebase tools, but you won't be able to use firebase-functions and its TypeScript bindings.
Or, you can lazily load your modules instead of statically loading them at the global scope of your functions, as described in this video.
I don't recommend using webpack. It's not going to be worth your time to get it configured.
You might try better-firebase-functions, which solves this elegantly by automatically lazy loading only the function that is currently invoked by checking the environment variable FUNCTION_NAME - see https://link.medium.com/4g3CJOLXidb
So, after reading a lot of documentation and tutorials, I realized that node.js can use "var http = require('http')" and "var fs = require('fs')" and some other code lines to serve html files but if it is hosted by Firebase Cloud Functions, that would not work because Firebase Cloud Functions works differently.
Can I get any simple code example how I can serve an html file index.html within node.js for Firebase Cloud Functions hosting?
You can simply host your index.html file by putting it in /public folder and deploy to Firebase Host. https://firebase.google.com/docs/hosting/quickstart
Steps are quick simple.
1. Install FirebaseCLI
2. Call $firebase init - to create the project and generate firebase.json, package.json, a public folder with .html files
3. modify the file and upload using the command $firebase deploy
This works for static (just HTML) or SPA (single page application)
Cloud Function is mostly used to build dynamic websites and API.
Command $firebase init functions after processing is not asking for my project id at firebase which should be initialized, instead it is using default project. Here is the picture
It is because you are reinitializing the firebase functions in the current directory i.e. learning chatbots.
If you intend to overwrite the existing firebase functions in this directory you can follow these instructions:
OR if don't want to overwrite existing files:
It will be better if you create a new directory and then initialize firebase functions there.
Make a new directory outside of "learning chatbots" directory and
try initializing there.