Firebase Web 应用程序的不同环境

Different environments on Firebase web application

我正在使用 Firebase 构建 Web 应用程序。目前我可以说我确实有两个阶段 - 开发,运行本地主机的 firebase serve 和将 Web 应用程序上传到 Firebase 托管上的 firebase deploy --only hosting

一切都很好,但我不认为这是一个专业的解决方案。我看到的问题是,我的本地环境和实时 Web 应用程序共享同一个数据库。我对该主题进行了相当多的研究,并且我了解到在 Firebase 上无法为每个项目拥有两个数据库。那里提供的解决方案是在 Firebase 上创建两个项目,一个用于开发,一个用于生产。或者,即使您愿意,也可以用于分期。

这个解决方案对我来说似乎完全没问题。这当然是个好主意。几个项目,几个环境,独立的数据库,非常完美。然后就在实施此解决方案之前,我想到了另一个问题。如果我说,让我们创建一个暂存项目,以便为我提供暂存环境,并且我决定部署我的 Web 应用程序,暂存 Web 应用程序将公开可用,因此它也会被 Google 索引等等。

那么,在这种情况下你能给我什么建议?我如何才能确保我的暂存 Web 应用程序(托管在暂存 Firebase 应用程序上)对其他人不可用并且不会被搜索引擎编入索引。我考虑过将 IP 或 VPC 列入白名单,但我不知道如何以 freereliable.[=12 的方式进行=]

编辑:以下解决方案适用于 Firebase "Realtime Database"。它不适用于 "Firestore"。读取差异 here.

1。 Firebase 实时数据库分片

现在(2018 年 3 月),Firebase 实时数据库允许您创建多个实例。

官方文档:Scale with Multiple Databases

  1. 转到您的 Firebase 项目

  2. 在 Firebase 控制台中,转到 Develop > Database 部分中的“数据”选项卡。 Select Create new database 来自“数据库”部分(右上角)的菜单。

  3. 自定义您的数据库引用和安全规则,然后单击知道了。

  4. (可选)修改新实例的Security ruleBackup option

2。用法

// Get the default database instance for an app
var database = firebase.database();

// Get a secondary database instance by URL
var database = firebase.database('https://testapp-1234.firebaseio.com');

3。用法示例:不同的环境

firebase-config.js

const BUILD_LEVEL = "dev";
// const BUILD_LEVEL = 'stage'
// const BUILD_LEVEL = 'prod'

let config = {
  apiKey: "your_apiKey",
  authDomain: "your_authDomain",
  projectId: "your_projectId",
  storageBucket: "your_storageBucket",
  messagingSenderId: "your_messagingSenderId"
};

if (BUILD_LEVEL === "dev") {
  config.databaseURL = "https://your-project-dev.firebaseio.com/";
} else if (BUILD_LEVEL === "stage") {
  config.databaseURL = "https://your-project-stage.firebaseio.com";
} else if (BUILD_LEVEL === "prod") {
  config.databaseURL = "https://your-project-dev.firebaseio.com";
}

firebase.initializeApp(config);

现在要更改 Firebase 数据库实例,只需更改 BUILD_LEVEL 变量即可。

将此功能与 Git/Github/Gitlab workflowGit hookwebpackCI/CD tool 相结合,您将获得一个非常灵活的解决方案。

如果有人有这个问题,Firebase 博客上有一个关于这个的 article

注意:这篇 Firebase 文章假定您已经为此新环境(即 project-dev)创建了第二个 Firebase 项目,并将配置详细信息复制到您的工作环境(即 project-dev)中。 Master 和 dev 是两个不同的环境,所以有两个不同的 Firebase 配置是有意义的。

文章指出:

Fortunately for us, the Firebase CLI makes it simple to setup and deploy to multiple environments.

Adding and switching between environments with the Firebase CLI is as simple as one command: firebase use.

$ firebase use --add This command prompts you to choose from one of your existing projects

Select the project you want to use for a different environment, and then give it an alias. The alias can really be whatever you want, but it’s common to use aliases like “development”, “staging”, or “production”.

Once you’ve created a new alias, it will be set as the current environment for deployment. Running firebase deploy will deploy your app to that environment.

Switching environments

If you want to switch to another environment, just provide the alias in the use command.

$ firebase use default # sets environment to the default alias

$ firebase use staging # sets environment to the staging alias

For a single command, you can also specify the environment using the -P flag:

$ firebase deploy -P staging # deploy to staging alias

希望对您有所帮助!