Add an option to use local Firebase emulators
See original GitHub issueAfter creating an app with react-scripts, initialising Firebase and installing ReactFire, there are still quite a few steps needed to be able to develop the app using the local Firebase emulators:
- Run
firebase emulators:start
to start the emulators. - Add
"proxy": "http://localhost:5000"
to package.json so that requests for/__/firebase/init.json
are proxied to the hosting emulator. - Load the config from the hosting emulator, then preload and configure the rest of the emulators, before rendering the app:
import firebase from 'firebase/app'
import ReactDOM from 'react-dom'
import {
FirebaseAppProvider,
preloadAuth,
preloadFirestore,
preloadFunctions,
} from 'reactfire'
const AUTH_DOMAIN = 'example.com'
const FUNCTIONS_REGION = 'europe-west2'
fetch('/__/firebase/init.json').then(async (response) => {
const firebaseConfig = await response.json()
if (process.env.NODE_ENV === 'production') {
firebaseConfig.authDomain = AUTH_DOMAIN
}
const firebaseApp = firebase.initializeApp(firebaseConfig)
await preloadFirestore({
firebaseApp,
setup: async (factory) => {
const firestore = factory()
if (process.env.NODE_ENV !== 'production') {
firestore.useEmulator('localhost', 8080)
}
//if (process.env.NODE_ENV === 'production') {
// await firestore.enablePersistence({ synchronizeTabs: true })
//}
return firestore
},
})
await preloadFunctions({
firebaseApp,
setup: (factory) => {
const functions = factory(FUNCTIONS_REGION)
if (process.env.NODE_ENV !== 'production') {
functions.useEmulator('localhost', 5001)
}
return functions
},
})
await preloadAuth({
firebaseApp,
setup: async (factory) => {
const auth = factory()
if (process.env.NODE_ENV !== 'production') {
auth.useEmulator('http://localhost:9099')
}
return auth
},
})
ReactDOM.unstable_createRoot(document.getElementById('root')).render(
<FirebaseAppProvider firebaseApp={firebaseApp} suspense={true}>
<App />
</FirebaseAppProvider>
)
})
It feels like much of that could be handled inside ReactFire, perhaps with a boolean emulators
property (or a config object with values for each emulator as needed) on FirebaseAppProvider
. If it could avoid preloading each module until it’s needed for the first time that would be ideal.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:26
- Comments:6
Top Results From Across the Web
Install, configure and integrate Local Emulator Suite - Firebase
Install the Local Emulator Suite · Configure Emulator Suite · Start up emulators · Export and import emulator data · Integrate with your...
Read more >The Full Guide on how to use the Firebase Emulator for the Web
First, we'll go through the process of installing the emulator and the different APIs we'll use, like, Authentication, Firestore, and Functions. Second, we'll ......
Read more >Your quickstart to the Firebase Emulator Suite - YouTube
Local development with Firebase is faster when you use the Emulator Suite. Test your changes locally in a fraction of the time and...
Read more >Emulating Databases Locally
Use npm run em:start to run the emulators and start Firebase-like web console where you can add users or data. The options --import=./saved-data...
Read more >Setting Up Firebase Local Emulators using NodeJS - Medium
The Firebase Local Emulator Suite is a set of advanced tools for developers looking to build and test apps locally using Cloud Firestore, ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I agree with @hubgit suggestion, this would make the development experience better for all firebase products when using reactfire.
This would also align with the push in Firebase Summit 2020 #AskFirebase session which was pushing the emulators for use during dev.
Can you please add this to the backlog for someone to pickup and implement.
Cheers Bullfrog1234
I love the idea. But there would be one concern. Sometimes,
firebase emulators:start
throws errors on ports. So ideal would be to have ana ability to change the port too.