Search code examples
javascriptfirebasecorsgoogle-cloud-functionsfirebase-cli

Firebase emulator cloud function throws cors error when hit from frontend


I have this function that works perfectly in production however when running locally I get cors errors

export const listUsers = functions.https.onRequest(async (req, res) => {
    res.set("Access-Control-Allow-Origin", "*")
    const data = await getSomeUsers(10)
    res.json(data)
})

emulators starts fine:

enter image description here

cors issue:

enter image description here


Solution

  • Check this:

    Deadly CORS when http://localhost is the origin

    Chrome does not support localhost for CORS requests (a bug opened in 2010, marked WontFix in 2014).

    To get around this you can use a domain like lvh.me (which points at 127.0.0.1 just like localhost) or start chrome with the --disable-web-security flag (assuming you're just testing).