manifold/web/lib/firebase/init.ts

36 lines
1.2 KiB
TypeScript
Raw Normal View History

import { initializeApp, getApps, getApp } from 'firebase/app'
import { getStorage } from 'firebase/storage'
import { FIREBASE_CONFIG } from 'common/envs/constants'
2022-08-31 21:38:55 +00:00
import {
connectFirestoreEmulator,
initializeFirestore,
} from 'firebase/firestore'
import { connectFunctionsEmulator, getFunctions } from 'firebase/functions'
2021-12-08 16:42:19 +00:00
// Initialize Firebase
export const app = getApps().length ? getApp() : initializeApp(FIREBASE_CONFIG)
2022-08-31 21:38:55 +00:00
export const db = initializeFirestore(app, {
experimentalForceLongPolling: true,
})
export const functions = getFunctions()
export const storage = getStorage()
declare global {
/* eslint-disable-next-line no-var */
var EMULATORS_STARTED: boolean
}
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
if (!global.EMULATORS_STARTED) {
global.EMULATORS_STARTED = true
connectFirestoreEmulator(db, 'localhost', 8080)
connectFunctionsEmulator(functions, 'localhost', 5001)
}
}
if (process.env.NEXT_PUBLIC_FIREBASE_EMULATE) {
startEmulators()
}