'Cloud functions are getting called two times, first time with body and second time without

My index.ts has:

exports.foo = functions.https.onCall(async (data, context) => {
  console.log('Hello World');
  return null;
});

To deploy, I run:

firebase deploy --only functions:foo

To test, I do:

final callable = FirebaseFunctions.instance.httpsCallable('foo');
await callable.call();

First time when the function execution started, my function body runs, but the second time (don't know how it gets invoked), my function body doesn't run. Is this a standard behavior, am I also getting charged for the automatic second invocation?

enter image description here

NOTE: I've read several posts like this, this, this, this etc before asking this question but none of them seemed to work for me.



Solution 1:[1]

I've seen and more-or-less logged this; for example, recently some "minimum instances=1" functions seem to start-up and run a few times a day, but the function itself isn't invoked. I also see this at deploy time (I use some custom code that deploys multiple functions at a time).

The way "cold starts" work is they have to run the function files once to FIND and ASSIGN the "functions" within. This part used to be run silently. It would be nifty if Google either returned to NOT logging this, or differentiated it in the logs.

Solution 2:[2]

I don't know flutter, but you run it "as if you were in a browser". In addition, pressing a button usually submit something (I mean, it's not a GET request, most of the time).

So, the combination of both PLUS your issue lead me to think to a preflight request. Check the HTTP verb before performing the processing.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 LeadDreamer
Solution 2 guillaume blaquiere