長野エンジニアライフ

東京から長野に移住したエンジニアのブログです。🦒🗻⛰

HostingとFunctionのfirebase.jsonが異なりFunctionが呼べなくなった時の対処法

Firebase + LINE MessagingAPIを使ってメッセンジャーアプリを作成中、LINEからの受信に対して動いていたFunctionが動かなくなった。

  • Hostingのfirebase.json
{
  "hosting": {
    "public": "dist",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  }
}
  • Functionのfirebase.json
{
  "hosting": {
    "public": "dist",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "/webhook",
        "function": "app"
      }
    ]
  }
}

起こった問題

Hostingのfirebase.jsonをデプロイすると、/webhookがトリガとなるFunctionが呼ばれなくなってしまう。

原因

Hostingのfirebase.jsonをデプロイがFunction側のrewritesの箇所が上書きされてしまうため。

対処

firebase.jsonを以下に統一する。rewritesの記述する順番も重要

{
  "hosting": {
    "public": "dist",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "/webhook",
        "function": "app"
      },
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  }
}