Slide 1

Slide 1 text

React x Firebase を用いた Web アプリケーション開発時の ハマりどころと解決策 Vice President, RPG TEC tanabee

Slide 2

Slide 2 text

このスライドについて - Slide: http://bit.ly/slide1203 - Qiita: http://bit.ly/qiita1203

Slide 3

Slide 3 text

自己紹介 - Name: Yuki Tanabe - Licenses - Google Developers Expert ( G Suite category ) - Licensed Scrum Master - Social Accounts - GitHub: tanabee - Twitter: @_tanabee - Qiita: tanabee

Slide 4

Slide 4 text

概要 - これから React x Firebase の構成で Web アプリケーション 開発を始める方が同じところでハマらないようにハマりどこ ろと解決策を 4 つ紹介します。

Slide 5

Slide 5 text

1. Hosting と同じドメインで Cloud Functions の関数を 呼び出す

Slide 6

Slide 6 text

Hosting と同じドメインで Cloud Functions の関数を呼び出す https://[region]-[project id].cloudfunctions.net/[function name] Cloud Functions でデフォルトで生成される URL - https://[project id].web.app/[function name] - https://[project id].firebaseapp.com/[function name] - https://[独自ドメイン]/[function name] 以下のような URL で呼び出したい

Slide 7

Slide 7 text

Hosting と同じドメインで Cloud Functions の関数を呼び出す "hosting": { "rewrites": [ { "source": "/notifications", "function": "notifications" }, { "source": "**", "destination": "/index.html" } ] Hosting の設定ファイルで rewrites の設定をすれば OK https://firebase.google.com/docs/hosting/url-redirects-rewrites?hl=ja#section-rewrites firebase.json exports.notifications = functions .https .onRequest((req, res) => { // ... }; functions/index.js

Slide 8

Slide 8 text

2. ローカル環境の React から Emulator の Functions を 呼び出す

Slide 9

Slide 9 text

ローカル環境の React から Emulator の Functions を呼び出す - やりたかったこと - Functions をいちいちデプロイして検証したくない - yarn start で React のホットリロードはそのまま使いたい

Slide 10

Slide 10 text

const app = firebase.initializeApp(config) if (process.env.NODE_ENV === 'development') { app.functions().useFunctionsEmulator('http://localhost:5001') } Emulator Suite で Functions を起動し src/index.js を更新 src/index.js ローカル環境の React から Emulator の Functions を呼び出す https://firebase.google.com/docs/emulator-suite/connect_and_prototype?database=Firestore $ firebase emulators:start --only functions Command

Slide 11

Slide 11 text

3. React と Functions で Firestore の参照先を 合わせる

Slide 12

Slide 12 text

React と Functions で Firestore の参照先を合わせる - Firestore を使ったプロジェクトで 2 の設定を行った場合に 以下のような状態になる - React: Cloud の Firestore を参照 - Functions: Emulator の Firestore 参照 > Cloud か Emulator か参照先を統一する必要がある

Slide 13

Slide 13 text

React と Functions で Firestore の参照先を合わせる - 構成 1. Cloud の Firestore を参照する - React: yarn start - Functions: Emulator - Firestore: Cloud - メリデメ - 開発時に Firebase console からデータを確認できる - Cloud のデータが更新されるため、複数人開発には不 向き。リリース後は複数 PJ 構成などの工夫が必要

Slide 14

Slide 14 text

React と Functions で Firestore の参照先を合わせる https://firebase.google.com/docs/emulator-suite/connect_and_prototype?database=Firestore const Firestore = require('@google-cloud/firestore') const admin = require('firebase-admin') const db = (() => { if (process.env.FUNCTIONS_EMULATOR) { return new Firestore({ projectId: 'your project id' }) } else { return admin.firestore() } })() functions/index.js $ npm install --save @google-cloud/firestore Command

Slide 15

Slide 15 text

React と Functions で Firestore の参照先を合わせる https://cloud.google.com/docs/authentication/getting-started?hl=ja サービスアカウントキーを作成してローカルに配置 $ export GOOGLE_APPLICATION_CREDENTIALS="[PATH]" Command

Slide 16

Slide 16 text

React と Functions で Firestore の参照先を合わせる - 構成 2. Emulator の Firestore を参照する - React: yarn start - Functions: Emulator - Firestore: Emulator - メリデメ - ローカル環境に閉じてデータを変更できるので複数人開 発やリリース後の運用にも向いている - データの確認が難しい(Firestore の CLI がない?)

Slide 17

Slide 17 text

const app = firebase.initializeApp(config) if (process.env.NODE_ENV === 'development') { app.functions().useFunctionsEmulator('http://localhost:5001') db.settings({ host: "localhost:8081", ssl: false }); } src/index.js React と Functions で Firestore の参照先を合わせる https://firebase.google.com/docs/emulator-suite/connect_and_prototype?database=Firestore $ firebase emulators:start --only functions,firestore Command

Slide 18

Slide 18 text

4. Hosting から asia-northeast1 region の Functions を呼び出せない

Slide 19

Slide 19 text

Hosting から asia-northeast1 region の Functions を呼び出せない exports.getEvents = functions .region(‘asia-northeast1’) .https() .onCall((data) => { // ... }); region 指定すると Hosting から呼べなくなる

Slide 20

Slide 20 text

Hosting から asia-northeast1 region の Functions を呼び出せない https://firebase.google.com/docs/hosting/functions?hl=ja

Slide 21

Slide 21 text

Hosting から asia-northeast1 region の Functions を呼び出せない - Hosting とつなぐものは、現状 region 指定しないで Functions を運用するしかなさそう

Slide 22

Slide 22 text

ありがとうございました!