10
  • I have a React app that I'd like to deploy to multiple environments (dev, staging, prod).
  • each env is a separate Firebase project with its own config (apiKey, databseURL, etc). currently I hold multiple firebase-config.json for each env and use a switch in source code to determine which to use.
  • I am using Firebase hosting for the app.
  • The hosting docs state that I can fetch a special reserved url /__/firebase/init.json (or script in /__/firebase/init.js) that firebase hosting prepares for each project separately so I can init the correct instance of the firebase app for that environment. Also see the blog post here

What I can't figure out is: fetching the special js/json file over the network is async and in the meantime the react app is initialising and requires the firebase app for auth and other operations. Is there a way I can complete the firebase init and have a firebase app instance ready before react has started-up?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Shai Ben-Tovim
  • 892
  • 8
  • 20

2 Answers2

3

firebase setup:web --json has been deprecated.

You can now use firebase apps:sdkconfig web --json instead.

Full info here.

paintedbicycle
  • 287
  • 3
  • 13
2

Found this firebase-CLI command:

firebase setup:web --json

It will output the json required for initialising the firebase app based on the active firebase app (firebase use). Just need to include the resulting json in the build chain.

Shai Ben-Tovim
  • 892
  • 8
  • 20
  • Can you explain exactly how you came around to use it? It isn't clear to me. – mr.bjerre Sep 12 '19 at 18:06
  • Running this command will provide a json object that includes all of the data required to init a firebase app instance in code: `apiKey, projectId, etc`. You can use it to dynamically create a config file for your firebase init based on the firebase project being used. This way, when you switch projects, you can still have a valid init config for firebase – Shai Ben-Tovim Sep 14 '19 at 08:31
  • 2
    @mr.bjerre it's used in the example project https://github.com/firebase/firebaseui-web-react/blob/master/example. Specifically, it's in the `package.json` as a script, `createfirebaseconf`. – Michael Osofsky Oct 20 '19 at 16:18
  • 1
    @ShaiBen-Tovim `firebase setup:web` says "This command is deprecated. Instead, use 'firebase apps:sdkconfig web' to get web setup information." But I'm unable to run `firebase --non-interactive --token apps:sdkconfig --out firebase.json WEB ` because I get "FirebaseError: HTTP Error: 400, Request contains an invalid argument." in the firebase-debug.log. Does it work for you? – Michael Osofsky Oct 20 '19 at 16:21
  • 1
    This no longer works. Is there an updated way to do with with `apps:sdkconfig`? – paintedbicycle May 14 '20 at 19:41
  • Is there a way to make the new the new command output just the JSON instead of the comments and firebase.initializeApp? Something like `firebase apps:sdkconfig web --json` – mattgabor May 18 '20 at 05:30