Firebaseを使って静的なサイトの構築手順

firebaseを利用して、簡単サイトを制作して、デプロイするまでの手順です。
ローカル環境
- OS windows10 pro
- node V10.16.3
- npm 6.9.0
プロジェクト作成
こちらにアクセスして、コンソールへ移動をクリックします。
※googleアカウントが作成されているものとします。

「プロジェクトを作成」をクリックします。

任意のプロジェクト名を設定し、続行をクリックします。

今回はgoogleアナリティクスを無効にしたまま、プロジェクトを作成をクリックし、プロジェクトを作成します。

プロジェクト作成中の画面になります。

しばらくするとプロジェクトが作成されます。

firebase-toolsインストール
プロジェクトをローカルから操作するためにfirebase-toolsをインストールします。
npm i -g firebase-tools
ログインします。
firebase login
ログインするためのURLが静止されるので、アクセスしてログインします
i Firebase optionally collects CLI usage and error reporting information to help improve our products. Data is collected in accordance with Google's privacy policy (https://policies.google.com/privacy) and is not used to identify you.
? Allow Firebase to collect CLI usage and error reporting information? Yes
i To change your data collection preference at any time, run `firebase logout` and log in again.
Visit this URL on this device to log in:
https://accounts.google.com/o/oauth2/auth?XXXXXXXXXXXXXXXXXXXXX
お持ちのgoogleアカウントで認証すると下記の画面が表示され、認証が完了します。

静的サイトの作成
下記のコマンドで静的サイトを構築します。
firebase init
いくつか質問されるので、今回は下記のとおりに入力します。
yesと回答
Are you ready to proceed? Yes
? Which Firebase CLI features do you want to set up for this folder? Press Space to select features, then Enter to confirm your choices. Hosting: Configure and deploy Firebase Hosting sites
=== Project Setup
First, let's associate this project directory with a Firebase project.
You can create multiple project aliases by running firebase use --add,
but for now we'll just set up a default project.
作成したプロジェクトを選択
? Select a default Firebase project for this directory: test-72969 (test)
i Using project 作成したプロジェクト (test)
=== Hosting Setup
Your public directory is the folder (relative to your project directory) that
will contain Hosting assets to be uploaded with firebase deploy. If you
have a build process for your assets, use your build's output directory.
サイトのディレクトリはpubicとして、SPAはYesを選択してます。
? What do you want to use as your public directory? public
? Configure as a single-page app (rewrite all urls to /index.html)? Yes
+ Wrote public/index.html
完了すると、firebase initを実行した階層に
publicフォルダ
firebase.json
.firebaserc
.gitignore
が作成されます。
実行
下記のコマンドで一度実行してみます。
firebase serve
ブラウザから http://localhost:5000 にアクセスすると、下記の画面が表示されます。

デプロイ
最後にデプロイしてみます。
firebaseにアクセスしてプラットフォームにウェブを選択します。

javascriptが表示されるので、ローカルのpublic配下にあるindex.htmlに追加します。

index.htmlを下記のように編集します。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Welcome to Firebase Hosting</title>
<!-- update the version number as needed -->
<script defer src="/__/firebase/7.6.1/firebase-app.js"></script>
<!-- include only the Firebase features as you need -->
<script defer src="/__/firebase/7.6.1/firebase-auth.js"></script>
<script defer src="/__/firebase/7.6.1/firebase-database.js"></script>
<script defer src="/__/firebase/7.6.1/firebase-messaging.js"></script>
<script defer src="/__/firebase/7.6.1/firebase-storage.js"></script>
<!-- initialize the SDK after all desired features are loaded -->
<script defer src="/__/firebase/init.js"></script>
<style media="screen">
body { background: #ECEFF1; color: rgba(0,0,0,0.87); font-family: Roboto, Helvetica, Arial, sans-serif; margin: 0; padding: 0; }
#message { background: white; max-width: 360px; margin: 100px auto 16px; padding: 32px 24px; border-radius: 3px; }
#message h2 { color: #ffa100; font-weight: bold; font-size: 16px; margin: 0 0 8px; }
#message h1 { font-size: 22px; font-weight: 300; color: rgba(0,0,0,0.6); margin: 0 0 16px;}
#message p { line-height: 140%; margin: 16px 0 24px; font-size: 14px; }
#message a { display: block; text-align: center; background: #039be5; text-transform: uppercase; text-decoration: none; color: white; padding: 16px; border-radius: 4px; }
#message, #message a { box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24); }
#load { color: rgba(0,0,0,0.4); text-align: center; font-size: 13px; }
@media (max-width: 600px) {
body, #message { margin-top: 0; background: white; box-shadow: none; }
body { border-top: 16px solid #ffa100; }
}
</style>
<!-- The core Firebase JS SDK is always required and must be listed first -->
<script src="https://www.gstatic.com/firebasejs/7.6.1/firebase-app.js"></script>
<!-- TODO: Add SDKs for Firebase products that you want to use
https://firebase.google.com/docs/web/setup#available-libraries -->
<script>
// Your web app's Firebase configuration
var firebaseConfig = {
apiKey: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
authDomain: "xxxxx.firebaseapp.com",
databaseURL: "https://xxxxx.firebaseio.com",
projectId: "xxxxx",
storageBucket: "xxxxx.appspot.com",
messagingSenderId: "xxxxx",
appId: "xxxxx"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
</script>
</head>
<body>
<div id="message">
<h2>test</h2>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
// // 🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥
// // The Firebase SDK is initialized and available here!
//
// firebase.auth().onAuthStateChanged(user => { });
// firebase.database().ref('/path/to/ref').on('value', snapshot => { });
// firebase.messaging().requestPermission().then(() => { });
// firebase.storage().ref('/path/to/ref').getDownloadURL().then(() => { });
//
// // 🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥
try {
let app = firebase.app();
let features = ['auth', 'database', 'messaging', 'storage'].filter(feature => typeof app[feature] === 'function');
document.getElementById('load').innerHTML = `Firebase SDK loaded with ${features.join(', ')}`;
} catch (e) {
console.error(e);
document.getElementById('load').innerHTML = 'Error loading the Firebase SDK, check the console.';
}
});
</script>
</body>
</html>
デプロイします
firebase deploy
URLが表示されるので、アクセスします。
Opening Hosting: Deployed Site link in your default browser:
https://xxxxx.firebaseapp.com
ブラウザよりアクセスすると下記の画面が表示されると思います。

-
前の記事
AngularJSを使ってみる 2020.01.10
-
次の記事
Nuxt.js エラー「Enable vuex store by creating store/index.js.」発生時の対応方法 2020.01.13
コメントを書く