LoginSignup
12
6

More than 5 years have passed since last update.

Firebase Hosting から Functions への rewrite とパーマリンク [BitScheduler開発日誌]

Posted at

人生同様、モチベーションも山あり谷あり。
BitScheduler開発日誌、1年ぶりの6回目です。

今回は Firebase Hosting から rewrite 設定を使って CloudFunctions を呼び出す際のパーマリンクの受け渡し方法です。
Hosting では firebase.json に rewrite を設定でき、接続先に functions を指定できます。

rewrite による functions 接続の設定例

"hosting": {
    "public": "public",
    "rewrites": [
      {
        "source": "/home/**", "function": "home"
      },
      {
        "source": "/detail/**", "function": "detail"
      }
    ]
}

上記では Hosting への "/home" リクエストが functions の "/home" 関数呼び出しとなります。
その際、"**" を指定しているため、"/home" 配下のサブディレクトリの指定が home 関数にも渡ります。

リクエストが "/home/hoge" だった場合、リクエストパス(req.path)にはそのまま "/home/hoge" が割り当てられるため、home 関数に対するパーマリンクである "hoge" を取得する場合は下記コードになります。

パーマリンクの取得例

exports.home = functions.https.onRequest((req, res) => {
    let dir1 = req.path.split('/')[1];  // dir1 = 'home'
    let dir2 = req.path.split('/')[2];  // dir2 = 'hoge'
}

参考情報

Firebase Hosting デプロイ設定

BitSchedulerとは

開発中の出欠投稿サービスです。Googleカレンダーと連携することで簡単に管理、運用できることを目指しています。
ぜひ「いいね」して応援してください!

BitSchedulerランディングページ

12
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
6