將第 1 代 Node.js 函式升級至第 2 代

目前使用第 1 代函式的應用程式應採用本指南中的操作說明,考慮遷移至第 2 代。第 2 代函式使用 Cloud Run 來提供更好的效能、更好的設定、更好的監控等功能。

本頁中的範例假設您使用 JavaScript 搭配 CommonJS 模組 (require 樣式匯入),但相同的原則也適用於透過 ESM (import … from 樣式匯入) 和 TypeScript 的 JavaScript。

遷移程序

第 1 代和第 2 代函式可以在同一個檔案中並列顯示。這樣就能在準備就緒時分段輕鬆進行遷移。建議您一次遷移一個函式,並先執行測試和驗證,然後再繼續進行。

驗證 Firebase CLI 和 firebase-functions 版本

請確認您至少使用 Firebase CLI 版本 12.00firebase-functions 4.3.0。任何較新的版本都會支援第 2 代和第 1 代。

更新匯入作業

第 2 代函式會從 firebase-functions SDK 中的 v2 子套件匯入。這個不同的匯入路徑是在 Firebase CLI 判斷是否要部署第 1 代或第 2 代函式時所需的函式程式碼。

v2 子套件是模組化的,建議您僅匯入您需要的特定模組。

變更前:第 1 代

const functions = require("firebase-functions");

變更後:第 2 代

// explicitly import each trigger
const {onRequest} = require("firebase-functions/v2/https");
const {onDocumentCreated} = require("firebase-functions/v2/firestore");

更新觸發條件定義

由於第 2 代 SDK 偏好模組匯入,因此請更新觸發條件定義,以反映上一個步驟中已變更的匯入內容。

在某些觸發條件中傳遞到回呼的引數已變更。在此範例中,請注意 onDocumentCreated 回呼的引數已合併為單一 event 物件。此外,部分觸發條件有實用的新設定功能,例如 onRequest 觸發條件的 cors 選項。

變更前:第 1 代

const functions = require("firebase-functions");

exports.date = functions.https.onRequest((req, res) => {
  // ...
});

exports.uppercase = functions.firestore
  .document("my-collection/{docId}")
  .onCreate((change, context) => {
    // ...
  });

變更後:第 2 代

const {onRequest} = require("firebase-functions/v2/https");
const {onDocumentCreated} = require("firebase-functions/v2/firestore");

exports.date = onRequest({cors: true}, (req, res) => {
  // ...
});

exports.uppercase = onDocumentCreated("my-collection/{docId}", (event) => {
  /* ... */
});

使用參數化設定

第 2 代函式支援 functions.config,改用更安全的介面,在程式碼集中以宣告方式定義設定參數。使用新的 params 模組時,除非所有參數具有有效值,否則 CLI 會封鎖部署作業,確保部署函式時並未缺少設定。

遷移至 params 子套件

如果您一直在使用 functions.config 的環境設定,就可以將現有設定遷移至參數化設定

變更前:第 1 代

const functions = require("firebase-functions");

exports.date = functions.https.onRequest((req, res) => {
  const date = new Date();
  const formattedDate =
date.toLocaleDateString(functions.config().dateformat);

  // ...
});

變更後:第 2 代

const {onRequest} = require("firebase-functions/v2/https");
const {defineString} = require("firebase-functions/params");

const dateFormat = defineString("DATE_FORMAT");

exports.date = onRequest((req, res) => {
  const date = new Date();
  const formattedDate = date.toLocaleDateString(dateFormat.value());

  // ...
});

設定參數值

第一次部署時,Firebase CLI 會提示所有參數值,並將值儲存在 dotenv 檔案中。如要匯出 functions.config 值,請執行 firebase functions:config:export

如要加強安全性,您也可以指定參數 types驗證規則

特殊情況:API 金鑰

params 模組可與 Cloud Secret Manager 整合,可針對 API 金鑰等機密值提供精細的存取權控管機制。詳情請參閱密鑰參數

變更前:第 1 代

const functions = require("firebase-functions");

exports.getQuote = functions.https.onRequest(async (req, res) => {
  const quote = await fetchMotivationalQuote(functions.config().apiKey);
  // ...
});

變更後:第 2 代

const {onRequest} = require("firebase-functions/v2/https");
const {defineSecret} = require("firebase-functions/params");

// Define the secret parameter
const apiKey = defineSecret("API_KEY");

exports.getQuote = onRequest(
  // make the secret available to this function
  { secrets: [apiKey] },
  async (req, res) => {
    // retrieve the value of the secret
    const quote = await fetchMotivationalQuote(apiKey.value());
    // ...
  }
);

設定執行階段選項

執行階段選項的設定已在第 1 代和第 2 代之間變更。第 2 代還新增了一項功能,可用來設定所有函式的選項。

變更前:第 1 代

const functions = require("firebase-functions");

exports.date = functions
  .runWith({
    // Keep 5 instances warm for this latency-critical function
    minInstances: 5,
  })
  // locate function closest to users
  .region("asia-northeast1")
  .https.onRequest((req, res) => {
    // ...
  });

exports.uppercase = functions
  // locate function closest to users and database
  .region("asia-northeast1")
  .firestore.document("my-collection/{docId}")
  .onCreate((change, context) => {
    // ...
  });

變更後:第 2 代

const {onRequest} = require("firebase-functions/v2/https");
const {onDocumentCreated} = require("firebase-functions/v2/firestore");
const {setGlobalOptions} = require("firebase-functions/v2");

// locate all functions closest to users
setGlobalOptions({ region: "asia-northeast1" });

exports.date = onRequest({
    // Keep 5 instances warm for this latency-critical function
    minInstances: 5,
  }, (req, res) => {
  // ...
});

exports.uppercase = onDocumentCreated("my-collection/{docId}", (event) => {
  /* ... */
});

使用並行

第 2 代函式的一大優點在於,單一函式執行個體可一次處理多項要求。如此可大幅減少使用者遇到的冷啟動次數。根據預設,並行會設為 80,但您可以將其設為 1 到 1000 之間的任何值:

const {onRequest} = require("firebase-functions/v2/https");

exports.date = onRequest({
    // set concurrency value
    concurrency: 500
  },
  (req, res) => {
    // ...
});

調整並行可以提高效能並降低函式成本。如要進一步瞭解並行,請參閱允許並行要求

稽核全域變數使用情形

第 1 代在不採用並行的情況下編寫的函式可能會使用針對各要求設定及讀取的全域變數。在啟用並行的情況下,且單一執行個體一次開始處理多個要求時,隨著並行要求同時開始設定及讀取全域變數,這可能會在您的函式中產生錯誤。

升級時,您可以將函式的 CPU 設為 gcf_gen1,並將 concurrency 設為 1,還原第 1 代行為:

const {onRequest} = require("firebase-functions/v2/https");

exports.date = onRequest({
    // TEMPORARY FIX: remove concurrency
    cpu: "gcf_gen1",
    concurrency: 1
  },
  (req, res) => {
    // ...
});

不過,我們不建議長期修正問題,因為這麼做會減損第 2 代函式的效能優勢。請改為稽核函式中全域變數的使用情形,並在準備就緒時移除這些臨時設定。

將流量遷移至新的第 2 代函式

就像變更函式的區域或觸發條件類型一樣,您必須為第 2 代函式指定新名稱,並慢慢將流量遷移至該函式。

您無法使用相同名稱將函式從第 1 代升級至第 2 代,並執行 firebase deploy。這麼做會導致錯誤:

Upgrading from GCFv1 to GCFv2 is not yet supported. Please delete your old function or wait for this feature to be ready.

在執行下列步驟前,請先確認您的函式為冪等,因為新版和舊版函式會在變更同時同時執行。舉例來說,如果您的第 1 代函式會回應 Firestore 中的寫入事件,請務必針對第 1 代函式和第 2 代函式分別回應寫入兩次,以便回應這些事件,讓應用程式呈現一致的狀態。

  1. 重新命名函式程式碼中的函式。例如將 resizeImage 重新命名為 resizeImageSecondGen
  2. 部署函式,讓原本的第 1 代函式和第 2 代函式同時運作。
    1. 如果是可呼叫、工作佇列和 HTTP 觸發條件,請將用戶端程式碼更新為第 2 代函式的名稱或網址,開始將所有用戶端指向第 2 代函式。
    2. 透過背景觸發條件,第 1 代和第 2 代函式會在部署後立即回應每個事件。
  3. 關閉所有流量後,請使用 Firebase CLI 的 firebase functions:delete 指令刪除第 1 代函式。
    1. 您可以選擇重新命名第 2 代函式,以符合第 1 代函式的名稱。