alexluong / gatsby-packages

Gatsby packages
MIT License
56 stars 24 forks source link

Custom Firebase Functions region #27

Open davwheat opened 4 years ago

davwheat commented 4 years ago

The region for firebase functions must be declared at initialisation, but this package doesn't provide any way to give a region.

This is really important to reduce latency as much as possible.

https://firebase.google.com/docs/functions/locations#client-side_location_selection_for_callable_functions

yzalvov commented 4 years ago

@davwheat try this:

import firebase from 'gatsby-plugin-firebase'
firebase.functions = firebase.app().functions('europe-west1') // Your region here.
const myFunction = firebase.functions.httpsCallable(
  'myFunction'  // Your function name here.
)

// Then use your cloud function as usually.
davwheat commented 4 years ago

Ooh I didn't think about the fact that app was exported too! I'll try that.

yzalvov commented 4 years ago

One more thing is needed to have gatsby build successful. You'd have to use typeof window !== 'undefined' check as Gatsbyjs docs suggest at How to check if window is defined. So the code above would become:

import firebase from 'gatsby-plugin-firebase'
firebase.functions = typeof window !== 'undefined' && firebase.app().functions('europe-west1') // Your region here.
const myFunction = firebase.functions 
  ? firebase.functions.httpsCallable('myFunction') // Your function name here.
  : () => Promise.resolve(null)

// Then use your cloud function as usually.
alexluong commented 4 years ago

Hmm I'll take a look into this issue once I have the time. That seems a little annoying.

If that's the only way to go about this, would yall want me to do all these aliases and export them from gatsby-plugin-firebase so you won't have to do it?

davwheat commented 4 years ago

@yzalvov You can always just stick it in a useEffect, which looks much cleaner in my opinion.