在React中自动配置Firebase SDK [英] Auto config Firebase SDK in React

查看:76
本文介绍了在React中自动配置Firebase SDK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 我有一个我想部署到多个环境(开发,登台,生产)的React应用.
  • 每个env是一个单独的Firebase项目,具有自己的配置(apiKey,databseURL等).目前,我为每个环境持有多个firebase-config.json,并使用源代码中的一个开关来确定要使用哪个.
  • 我正在为该应用程序使用Firebase托管.
  • 托管文档指出我可以获取一个特殊的保留URL /__/firebase/init.json(或/__/firebase/init.js中的脚本),firebase托管将分别为每个项目准备该URL,以便我可以为该环境初始化firebase应用程序的正确实例.另请参见博客文章此处
  • 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

我不知道的是:通过网络获取特殊的js/json文件是异步的,与此同时,react应用正在初始化,并且需要Firebase应用进行身份验证和其他操作.有没有办法可以在响应启动之前完成firebase的初始化并准备好Firebase应用程序实例?

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?

推荐答案

找到此firebase-CLI命令:

Found this firebase-CLI command:

firebase setup:web --json

它将基于活动的Firebase应用程序(firebase use)输出初始化Firebase应用程序所需的json. 只需将生成的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.

这篇关于在React中自动配置Firebase SDK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆