Firebase Functions in Custom Region CORS Error
25th Oct 2021
I have been using Firebase again recently to build a simple app for Esther who is a Piano teacher. I've not used it for several years and things have definitely come a long way. The ability to use serverless functions is really exciting.
I am based in England and so I am using the europe-west2
region to deploy my functions but when I tried to call the function from my browser code I got a CORS error.
I spent hours trying to track down what was going on, getting more confused when I found the { origin: true }
given to the cors middleware.
And then the answer came at the bottom of this issue thread:
https://github.com/firebase/functions-samples/issues/395#issuecomment-635445763
If you are deploying functions to a region which is not the default region you need to specify the custom region in the client-side code otherwise that function effectively doesn't exist. This explains why we see CORS errors.
The fix looks like this:
export const functions =getFunctions(clientApp, FUNCTION_REGION)
Once this was set, the functions worked as expected.