Providers

Firebase

将 Nitro 应用部署到 Firebase。

你需要使用 Blaze plan(即用即付)才能开始使用。

Firebase 应用托管

¥Firebase app hosting

预设:firebase_app_hosting

¥Preset: firebase_app_hosting

Read more in Firebase App Hosting.
你可以使用 zero configuration 与此提供商集成。

项目设置

¥Project setup

  1. 转到 Firebase console 并设置一个新项目。
  2. 从侧边栏中选择“构建”>“应用托管”。
    • 你可能需要在此步骤升级你的计费方案。
  3. 点击“开始”。
    • 选择一个区域。
    • 导入 GitHub 代码库(你需要关联你的 GitHub 账户)。
    • 配置部署设置(项目根目录和分支),并启用自动部署。
    • 为你的后端选择一个唯一的 ID。
  4. 点击“完成并部署”以创建你的第一个部署。

使用 Firebase 应用托管部署时,应用托管预设将在构建时自动运行。

¥When you deploy with Firebase App Hosting, the App Hosting preset will be run automatically at build time.

Firebase 托管(已弃用)

¥Firebase hosting (deprecated)

::important 此部署方法已弃用,不推荐使用。Firebase App Hosting 是在 Firebase 上部署 Nitro 应用的推荐方式。::

¥::important This deployment method is deprecated and is not recommended. Firebase App Hosting is the recommended way to deploy Nitro apps on Firebase. ::

预设:firebase

¥Preset: firebase

Read more in Firebase Hosting.

::important 此预设将默认部署到 Firebase Functions 第一代。如果你想部署到 Firebase Functions 第二代,请参阅 以下说明。::

¥::important This preset will deploy to firebase functions 1st gen by default. If you want to deploy to firebase functions 2nd gen, see the instructions below. ::

项目设置

¥Project Setup

使用 Firebase CLI(推荐)

¥Using firebase CLI (recommended)

你可能更倾向于使用 Firebase CLI 设置项目,它将为你获取项目 ID、添加所需的依赖(参见上文),甚至通过 GitHub Actions 设置自动部署(仅用于托管)。了解如何安装 Firebase CLI

¥You may instead prefer to set up your project with the Firebase CLI, which will fetch your project ID for you, add required dependencies (see above) and even set up automated deployments via GitHub Actions (for hosting only). Learn about installing the firebase CLI.

  1. 全局安装 Firebase CLI

请始终尝试使用最新版本的 Firebase CLI。

¥Always try to use the latest version of the Firebase CLI.

npm install -g firebase-tools@latest

注意:你需要在 ^11.18.0 上部署 nodejs18 函数。

¥Note: You need to be on ^11.18.0 to deploy a nodejs18 function.

  1. 初始化你的 Firebase 项目
firebase login
firebase init hosting

出现提示时,你可以输入 .output/public 作为公共目录。在下一步中,请勿将你的项目配置为单页应用。

¥When prompted, you can enter .output/public as the public directory. In the next step, do not configure your project as a single-page app.

完成后,将以下内容添加到你的 firebase.json 中,以便在 Cloud Functions 中启用服务器渲染:

¥Once complete, add the following to your firebase.json to enable server rendering in Cloud Functions:

firebase.json
{
  "functions": { "source": ".output/server" },
  "hosting": [
    {
      "site": "<your_project_id>",
      "public": ".output/public",
      "cleanUrls": true,
      "rewrites": [{ "source": "**", "function": "server" }]
    }
  ]
}

你可以在 Firebase 文档 中找到更多详细信息。

¥You can find more details in the Firebase documentation.

替代方法

¥Alternative method

如果你的根目录中还没有 firebase.json 文件,Nitro 会在你第一次运行时创建一个。在此文件中,你需要将 <your_project_id> 替换为你的 Firebase 项目的 ID。然后应将此文件提交到 git。

¥If you don't already have a firebase.json in your root directory, Nitro will create one the first time you run it. In this file, you will need to replace <your_project_id> with the ID of your Firebase project. This file should then be committed to the git.

  1. 创建 .firebaserc 文件

建议创建一个 .firebaserc 文件,这样你就无需手动将项目 ID 传递给 firebase 命令(使用 --project <your_project_id> 时):

¥It is recommended to create a .firebaserc file so you don't need to manually pass your project ID to your firebase commands (with --project <your_project_id>):

.firebaserc
{
  "projects": {
    "default": "<your_project_id>"
  }
}

此文件通常在你使用 Firebase CLI 初始化项目时生成。如果你没有资源包,可以手动创建。

¥This file is usually generated when you initialize your project with the Firebase CLI. But if you don't have one, you can create it manually.

  1. 安装 Firebase 依赖

然后,将 Firebase 依赖添加到你的项目中:

¥Then, add Firebase dependencies to your project:

npm i firebase-admin firebase-functions firebase-functions-test
  1. 登录 Firebase CLI

确保你已通过 Firebase CLI 进行身份验证。运行此命令并按照提示操作:

¥Make sure you are authenticated with the firebase cli. Run this command and follow the prompts:

npx firebase-tools login

本地预览

¥Local preview

如果你需要在不部署的情况下进行测试,可以预览网站的本地版本。

¥You can preview a local version of your site if you need to test things out without deploying.

NITRO_PRESET=firebase npm run build
firebase emulators:start

构建和部署

¥Build and deploy

通过运行 Nitro 构建,然后运行 ​​firebase deploy 命令,部署到 Firebase Hosting。

¥Deploy to Firebase Hosting by running a Nitro build and then running the firebase deploy command.

NITRO_PRESET=firebase npm run build
npx firebase-tools deploy

如果你全局安装了 Firebase CLI,也可以运行:

¥If you installed the Firebase CLI globally, you can also run:

firebase deploy

使用第二代 Firebase 函数

¥Using 2nd generation firebase functions

要切换到较新且推荐的 Firebase 函数版本,请将 firebase.gen 选项设置为 2

¥To switch to the more recent and, recommended generation of firebase functions, set the firebase.gen option to 2:

export default defineNitroConfig({
  firebase: {
    gen: 2
    // ...
  }
})
如果你因任何原因无法使用配置,也可以使用 NITRO_FIREBASE_GEN 环境变量。

如果你已经部署了网站的某个版本,并希望升级到第二代 查看 Firebase 文档中的迁移过程。也就是说,CLI 会要求你在部署新函数之前删除现有函数。

¥If you already have a deployed version of your website and want to upgrade to 2nd gen, see the Migration process on Firebase docs. Namely, the CLI will ask you to delete your existing functions before deploying the new ones.

选项

¥Options

你可以在 nitro.config.ts 文件中设置 Firebase 函数的选项:

¥You can set options for the firebase functions in your nitro.config.ts file:

export default defineNitroConfig({
  firebase: {
    gen: 2,
    httpsOptions: {
      region: 'europe-west1',
      maxInstances: 3,
    },
  },
});

如果 gen 选项设置为 1,你还可以设置第一代云函数的选项。请注意,这些选项与第二代 Cloud Functions 的选项不同。

¥You can also set options for 1st generation Cloud Functions if the gen option is set to 1. Note these are different from the options for 2nd generation Cloud Functions.

运行时 Node.js 版本

¥Runtime Node.js version

你可以在配置中设置自定义 Node.js 版本:

¥You can set custom Node.js version in configuration:

export default defineNitroConfig({
  firebase: {
    nodeVersion: "20" // Can be "16", "18", "20" or "22"
  },
});

Firebase 工具在 package.json 中使用 engines.node 版本来确定你的函数应使用哪个节点版本。Nitro 自动将配置的 Node.js 版本写入 .output/server/package.json

¥Firebase tools use the engines.node version in package.json to determine which node version to use for your functions. Nitro automatically writes to the .output/server/package.json with configured Node.js version.

你可能还需要在 firebase.json 文件中添加运行时密钥:

¥You might also need to add a runtime key to your firebase.json file:

firebase.json
{
  "functions": {
    "source": ".output/server",
    "runtime": "nodejs20"
  }
}

你可以在 Firebase 文档 中阅读更多相关信息。

¥You can read more about this in Firebase Docs.

如果你的 Firebase 项目包含其他云函数

¥If your firebase project has other cloud functions

部署 nitro 项目时,你可能会收到警告,其他云函数将被删除。这是因为 nitro 会将你的整个项目部署到 Firebase 函数。如果你只想部署你的 nitro 项目,可以使用 --only 标志:

¥You may be warned that other cloud functions will be deleted when you deploy your nitro project. This is because nitro will deploy your entire project to firebase functions. If you want to deploy only your nitro project, you can use the --only flag:

firebase deploy --only functions:server,hosting

高级配置

¥Advanced

重命名函数

¥Renaming function

在同一个 Firebase 项目中部署多个应用时,必须为服务器指定一个唯一的名称,以免覆盖函数。

¥When deploying multiple apps within the same Firebase project, you must give your server a unique name in order to avoid overwriting your functions.

你可以在配置中为已部署的 Firebase 函数指定新名称:

¥You can specify a new name for the deployed Firebase function in your configuration:

export default defineNitroConfig({
  firebase: {
    serverFunctionName: "<new_function_name>"
  }
})

::important firebase.serverFunctionName 必须是有效的 JS 变量名,并且不能包含破折号(-)。::

¥::important firebase.serverFunctionName must be a valid JS variable name and cannot include dashes (-). ::