2

I am using functions and hosting from firebase.

I have defined one function as shown below.

const functions = require("firebase-functions")
const cors = require('cors')

exports.hello = functions.https.onRequest((request, response) => {
  functions.logger.info("Hello logs!", {structuredData: true})
  return cors()(request, response, () => {
    response.send({data: 'hello fire functions'})
  })
})

And in hosting call the function like this:

import firebase from "firebase/app"
import "firebase/functions"

const config = { ... }
firebase.initializeApp( config )

const test = firebase.functions().httpsCallable('hello')

test().then( result => console.log(result) )

Then the functions log will be written twice as follows:

2:37:07.548 PM hello: Function execution started
2:37:07.599 PM hello: Hello logs!
2:37:07.600 PM hello: Function execution took 53 ms, finished with status code: 204

2:37:07.809 PM hello: Function execution started
2:37:07.816 PM hello: Hello logs!
2:37:07.817 PM hello: Function execution took 8 ms, finished with status code: 200

It is also displayed twice in the usage graph.

This behavior means I have to pay twice as much usage. This is not normal.

If cors is not used, the log and usage graph will show that it has been executed only once.

But if you don't use cors: When you call a function in the browser, the function is executed, but the browser gets a CORS error.

How can I solve this problem? I couldn't find a solution in the official documentation. (This is a problem after hosting and functions deploying. It is not a localhost environment.)

niceplugin
  • 23
  • 3

2 Answers2

0

Firstly, you are mixing up HTTP requests on the client with callable functions. That's not what you're supposed to do. Please review the documentation for both HTTP functions and callable functions to see how they are different. If you're using the callable SDK on the client, you should use a callable function on the backend.

Second, this is the normal expected behavior. Callable functions use CORS between the client and server. CORS clients issue a preflight request which causes the first request and first log. Then, the actual request which causes the second request and second log. You cannot avoid this when using CORS - that's simply how the protocol works.

See also:

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Thanks for the helpful answer. If I write the `onCall` function and call it once using `httpsCallable()` from the client, the usage is counted as 2. And this cannot be changed. Is it correct what I understood? – niceplugin Apr 03 '21 at 07:49
  • Yes, this is the way things work with CORS. – Doug Stevenson Apr 03 '21 at 14:57
  • Hey Doug how can I differentiate between the first request and second request. I am calling a payment processor and this double request behavior is messing up the process. – Taio Dec 27 '22 at 08:47
0

If you're using Firebase Hosting, you probably don't need CORS.
You can avoid preflight requests by adding rewrites to firebase.json.
However, there are conditions such as the position of the function must be us-central1. https://firebase.google.com/docs/hosting/functions

M.M.
  • 186
  • 1
  • 10