question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add an option to use local Firebase emulators

See original GitHub issue

After 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:

  1. Run firebase emulators:start to start the emulators.
  2. Add "proxy": "http://localhost:5000" to package.json so that requests for /__/firebase/init.json are proxied to the hosting emulator.
  3. 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:open
  • Created 3 years ago
  • Reactions:26
  • Comments:6

github_iconTop GitHub Comments

6reactions
Bullfrog1234commented, Jan 4, 2021

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

1reaction
subhendukunducommented, Feb 2, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found