2022-01-21 23:21:46 +00:00
import { initializeApp , getApps , getApp } from 'firebase/app'
2022-07-14 16:44:52 +00:00
import { getStorage } from 'firebase/storage'
2022-05-09 13:04:36 +00:00
import { FIREBASE_CONFIG } from 'common/envs/constants'
2022-08-31 21:38:55 +00:00
import {
connectFirestoreEmulator ,
initializeFirestore ,
} from 'firebase/firestore'
2022-04-25 15:46:35 +00:00
import { connectFunctionsEmulator , getFunctions } from 'firebase/functions'
2021-12-24 21:06:01 +00:00
2021-12-08 16:42:19 +00:00
// Initialize Firebase
2022-03-09 02:43:30 +00:00
export const app = getApps ( ) . length ? getApp ( ) : initializeApp ( FIREBASE_CONFIG )
2022-08-31 21:38:55 +00:00
2022-09-01 13:01:02 +00:00
function iOS() {
if ( typeof navigator === 'undefined' ) {
2022-09-01 13:01:49 +00:00
// We're on the server, proceed normally
2022-09-01 13:01:02 +00:00
return false
}
return (
[
'iPad Simulator' ,
'iPhone Simulator' ,
'iPod Simulator' ,
'iPad' ,
'iPhone' ,
'iPod' ,
] . includes ( navigator . platform ) ||
// iPad on iOS 13 detection
( navigator . userAgent . includes ( 'Mac' ) && 'ontouchend' in document )
)
}
2022-09-01 13:01:49 +00:00
// Long polling is necessary for ios, see: https://github.com/firebase/firebase-js-sdk/issues/6118
2022-09-01 13:01:02 +00:00
const opts = iOS ( ) ? { experimentalForceLongPolling : true } : { }
export const db = initializeFirestore ( app , opts )
2022-04-25 15:46:35 +00:00
export const functions = getFunctions ( )
2022-07-14 16:44:52 +00:00
export const storage = getStorage ( )
2022-01-21 23:21:46 +00:00
2022-05-26 21:41:24 +00:00
declare global {
/* eslint-disable-next-line no-var */
var EMULATORS_STARTED : boolean
}
2022-04-25 15:46:35 +00:00
function startEmulators() {
// I don't like this but this is the only way to reconnect to the emulators without error, see: https://stackoverflow.com/questions/65066963/firebase-firestore-emulator-error-host-has-been-set-in-both-settings-and-usee
2022-05-26 21:41:24 +00:00
if ( ! global . EMULATORS_STARTED ) {
global . EMULATORS_STARTED = true
2022-04-25 15:46:35 +00:00
connectFirestoreEmulator ( db , 'localhost' , 8080 )
connectFunctionsEmulator ( functions , 'localhost' , 5001 )
}
}
if ( process . env . NEXT_PUBLIC_FIREBASE_EMULATE ) {
startEmulators ( )
}