Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Basic Firebase / Cloud Functions v2 / CORS question

So, I managed to call a Google Cloud Function v1 function using httpsCallable on the web app (in both dev/local, and prod) and then the corresponding onCall usage in my cloud function definition -- I don't get any CORS complaints and the function call works.

However, now that I'm upgrading to cloud functions v2 I am getting blocked by CORS

I am getting this error in dev, when running the React web app locally trying to call a deployed v2 cloud function:

Access to a fetch at 'https://<function URL>' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

The client code is as follows:

const testGen2func = async () => {
    const func = httpsCallable(functions, "testGen2bfunc")

    const result = await func({ value: "hello" })
    console.log("testGen2func", result)
}
 
The cloud function is as follows:
exports.testGen2bFunc = onCall(
    { region: AUSTRALIA_SOUTHEAST1, enforceAppCheck: true, cors: false },
    (data) => {
        console.log("data", data)
    }
)
 
package versions, in both main React app, and functions folder:
+-- firebase-admin@10.3.0
+-- firebase-functions@4.4.0
+-- firebase-tools@12.0.0
 
I have no clue why this is being blocked. I thought onCall was meant to deal with CORS. I also tried various value for the onCall 'cors attribute, incl. true, false, and corsWhitelist, which is defined as follows:
const corsWhitelist = ["http://localhost:3000"]
 
Can anyone please help me sort this out?
Solved Solved
0 3 8,483
1 ACCEPTED SOLUTION

Well, wouldn't you know...I changed things around like you suggested and that didn't work, then when I changed it back it worked -- but i'd stupidly used a lowercase 'f' in the client code and uppercase 'F' in the server function def. I will give you the prize of accepting this as a solution, not because it was the solution, but maybe people like me can check spelling/case better in the future before posting 🙂

View solution in original post

3 REPLIES 3
Top Labels in this Space
Top Solution Authors