-
-
Save wataruoguchi/d4e95ff30bba8d86b2b8401d33fc9c6c to your computer and use it in GitHub Desktop.
Firebase公式動画を参考にFirebaseとNuxt.jsを使ってSSRをServerlessで実現する ref: https://qiita.com/wataruoguchi/items/fa9b59e21c4212293c04
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ # プロジェクト用のフォルダ(<proj>、任意に決めてください)を作って、そのフォルダの中へ移動。 | |
$ mkdir <proj> && cd $_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ sudo firebase serve --only functons,hosting |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ # <proj>フォルダにいることを確認してください。 | |
$ # src/nuxt.config.jsを編集します。 | |
$ vi src/nuxt.config.js |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ # <proj>フォルダにいることを確認してください。 | |
$ vi functions/index.js |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ # <proj>フォルダにいることを確認してください。 | |
$ # srcフォルダへ移動してビルドをします。 | |
$ cd src && yarn build |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ # <proj>フォルダに移動して静的アセットをコピー | |
$ cd .. && cp -R functions/nuxt/dist/ public && cp -R src/static/* public | |
$ # localhostで実行 | |
$ sudo firebase serve --only hosting,functions |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ # <proj>フォルダにいることを確認してください。 | |
$ firebase deploy |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"rewrites": [ | |
{ | |
"source": "**", | |
"function": "nuxtApp" | |
} | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ # <proj>フォルダにいることを確認してください。 | |
$ # firebase.json を編集します。 | |
$ vi firebase.json |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ # vue-cliとyarnをインストールします。 | |
$ sudo npm i -g vue-cli yarn | |
$ # nuxt-community/starter-templateを元にsrcフォルダへNuxtアプリが自動設定されます。 | |
$ vue init nuxt-community/starter-template src | |
$ # 質問への回答は全部デフォルトでいいかと思います。Enterを押し続けてください。 | |
- ? Generate project in current directory? | |
- ? Project name | |
- ? Project description | |
- ? ? Author | |
$ # srcフォルダへ移動し、npmパッケージをインストールします。 | |
$ cd src/ && yarn |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ # nuxt.config.jsという設定ファイルを編集します。 | |
$ vi nuxt.config.js |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
publicPath: '/public/', | |
vendor: ['axios'], | |
extractCSS: true, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ # srcフォルダにいることを確認してください。 | |
$ # 動画ではHTTPリクエストにisomorphic-fetchを使っています。 | |
$ # しかしNuxt.js公式がaxiosの使用を超勧めていますので長い物には巻かれます。 | |
$ yarn add axios | |
$ # デフォルトのindex.vueの中身を消して、一から作ります。 | |
$ echo '' > pages/index.vue && vi pages/index.vue |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ # srcフォルダにいることを確認してください。 | |
$ # yarn devでdev webサーバを起動して、Nuxt.jsも起動させます。 | |
$ yarn dev |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vendor: ['axios', 'babel-polyfill'], |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ # srcフォルダにいることを確認してください。 | |
$ # yarn devでdev webサーバを起動して、Nuxt.jsも起動させます。 | |
$ yarn dev |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
babel: { | |
presets: [ | |
'es2015', | |
'stage-0' | |
], | |
plugins: [ | |
['transform-runtime', { | |
'polyfill': true, | |
'regenerator': true, | |
}] | |
], | |
}, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ # srcフォルダにいることを確認してください。 | |
$ # babelプラグインをインストールします。 | |
$ yarn add -D babel-plugin-module-resolver babel-plugin-transform-runtime babel-preset-es2015 babel-preset-stage-0 | |
$ # nuxt.config.jsという設定ファイルを編集します。 | |
$ vi nuxt.config.js |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ # プロジェクトを選択できるよう追加します。 | |
$ firebase use --add |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ # <proj>フォルダに移動します。 | |
$ cd <proj> | |
$ # firebase-toolsをインストールします。 | |
$ sudo npm i -g firebase-tools | |
$ # ここでユーザログインをします。firebase公式のチュートリアルに沿って操作しました。 | |
$ firebase login | |
$ # Cloud Functionsを含め、初期設定を行います。 | |
$ firebase init functions | |
$ # プロジェクトが無いと思うので[create a new project]を選択します。 | |
- ? Select a default Firebase project for this directory: | |
$ # どの言語(JSかTS)を使いたいか聞かれます。以降の質問に対してはデフォルトで良いです。 | |
- ? What language would you like to use to write Cloud Functions? | |
- ? Do you want to use ESLint to catch probable bugs and enforce style? | |
- ? Do you want to install dependencies with npm now? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ # functonsフォルダに移動します。 | |
$ cd functions | |
$ # package.jsonを編集します。 | |
$ vi package.json |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ # 追加したパッケージをインストールします。 | |
$ yarn | |
$ # express をインストールします。 | |
$ yarn add express | |
$ # srcフォルダに移動し、アプリをビルドし、functionsフォルダに戻ってきましょう。 | |
$ cd ../src/ && yarn build && cd ../functions/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
buildDir: '../functions/nuxt', |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ # nuxtの設定ファイルを編集します。 | |
$ vi src/nuxt.config.js |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const functions = require('firebase-functions') | |
const express = require('express') | |
const { Nuxt } = require('nuxt') | |
const app = express() | |
const config = { | |
dev: false, | |
buildDir: 'nuxt', | |
build: { | |
publicPath: '/public/' | |
} | |
} | |
const nuxt = new Nuxt(config) | |
function handleRequest(req, res) { | |
res.set('Cache-Control', 'public, max-age=600, s-maxage=1200') | |
nuxt.renderRoute('/').then(result => { | |
res.send(result.html) | |
}).catch(e => { | |
res.send(e) | |
}) | |
} | |
app.get('*', handleRequest) | |
exports.nuxtApp = functions.https.onRequest(app) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ # functionsフォルダにいることを確認してください。 | |
$ echo '' > index.js && vi index.js |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ # ページは全てNuxt.jsがレンダリングするので、標準でできたファイルは消しましょう。 | |
$ rm public/404.html public/index.html | |
$ # functions/nuxtからdistをpublic/assetsにコピーしましょう。 | |
$ cp -R functions/nuxt/dist public/assets | |
$ # src/static/からもpublicにコピーしましょう。 | |
$ cp -R src/static/* public |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ # functionsフォルダにいることを確認してください。 | |
$ # <proj>フォルダに移動します。 | |
$ cd ../ | |
$ firebase init hosting | |
$ # 質問に対する回答は全部デフォルトで良いです。 | |
- ? What do you want to use as your public directory? | |
- ? Configure as a single-page app (rewrite all urls to /index.html)? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ # プロジェクト用のフォルダ(<proj>、任意に決めてください)を作って、そのフォルダの中へ移動。 | |
$ mkdir <proj> && cd $_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ # vue-cliとyarnをインストールします。 | |
$ sudo npm i -g vue-cli yarn | |
$ # nuxt-community/starter-templateを元にsrcフォルダへNuxtアプリが自動設定されます。 | |
$ vue init nuxt-community/starter-template src | |
$ # 質問への回答は全部デフォルトでいいかと思います。Enterを押し続けてください。 | |
- ? Generate project in current directory? | |
- ? Project name | |
- ? Project description | |
- ? ? Author | |
$ # srcフォルダへ移動し、npmパッケージをインストールします。 | |
$ cd src/ && yarn |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ # srcフォルダにいることを確認してください。 | |
$ # yarn devでdev webサーバを起動して、Nuxt.jsも起動させます。 | |
$ yarn dev |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ # <proj>フォルダに移動します。 | |
$ cd <proj> | |
$ # firebase-toolsをインストールします。 | |
$ sudo npm i -g firebase-tools | |
$ # ここでユーザログインをします。firebase公式のチュートリアルに沿って操作しました。 | |
$ firebase login | |
$ # Cloud Functionsを含め、初期設定を行います。 | |
$ firebase init functions | |
$ # プロジェクトが無いと思うので[create a new project]を選択します。 | |
- ? Select a default Firebase project for this directory: | |
$ # どの言語(JSかTS)を使いたいか聞かれます。以降の質問に対してはデフォルトで良いです。 | |
- ? What language would you like to use to write Cloud Functions? | |
- ? Do you want to use ESLint to catch probable bugs and enforce style? | |
- ? Do you want to install dependencies with npm now? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ # プロジェクトを選択できるよう追加します。 | |
$ firebase use --add |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ # nuxtの設定ファイルを編集します。 | |
$ vi src/nuxt.config.js |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ # functonsフォルダに移動します。 | |
$ cd functions | |
$ # package.jsonを編集します。 | |
$ vi package.json | |
$ # package.jsonを編集します。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ # 追加したパッケージをインストールします。 | |
$ yarn | |
$ # express をインストールします。 | |
$ yarn add express | |
$ # srcフォルダに移動し、アプリをビルドし、functionsフォルダに戻ってきましょう。 | |
$ cd ../src/ && yarn build && cd ../functions/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ # functionsフォルダにいることを確認してください。 | |
$ echo '' > index.js && vi index.js |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ # functionsフォルダにいることを確認してください。 | |
$ # <proj>フォルダに移動します。 | |
$ cd ../ | |
$ firebase init hosting | |
$ # 質問に対する回答は全部デフォルトで良いです。 | |
- ? What do you want to use as your public directory? | |
- ? Configure as a single-page app (rewrite all urls to /index.html)? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ # srcフォルダにいることを確認してください。 | |
$ # 動画ではHTTPリクエストにisomorphic-fetchを使っています。 | |
$ # しかしNuxt.js公式がaxiosの使用を超勧めていますので長い物には巻かれます。 | |
$ yarn add axios | |
$ # デフォルトのindex.vueの中身を消して、一から作ります。 | |
$ echo '' > pages/index.vue && vi pages/index.vue |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ # ページは全てNuxt.jsがレンダリングするので、標準でできたファイルは消しましょう。 | |
$ rm public/404.html public/index.html | |
$ # functions/nuxtからdistをpublic/assetsにコピーしましょう。 | |
$ cp -R functions/nuxt/dist public/assets | |
$ # src/static/からもpublicにコピーしましょう。 | |
$ cp -R src/static/* public |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ # <proj>フォルダにいることを確認してください。 | |
$ # firebase.json を編集します。 | |
$ vi firebase.json |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ sudo firebase serve --only functons,hosting |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ # <proj>フォルダにいることを確認してください。 | |
$ # src/nuxt.config.jsを編集します。 | |
$ vi src/nuxt.config.js |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ # <proj>フォルダにいることを確認してください。 | |
$ vi functions/index.js |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ # <proj>フォルダにいることを確認してください。 | |
$ # srcフォルダへ移動してビルドをします。 | |
$ cd src && yarn build |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ # <proj>フォルダに移動して静的アセットをコピー | |
$ cd .. && cp -R functions/nuxt/dist/ public && cp -R src/static/* public | |
$ # localhostで実行 | |
$ sudo firebase serve --only hosting,functions |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ # <proj>フォルダにいることを確認してください。 | |
$ firebase deploy |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ # nuxt.config.jsという設定ファイルを編集します。 | |
$ vi nuxt.config.js |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ # srcフォルダにいることを確認してください。 | |
$ # yarn devでdev webサーバを起動して、Nuxt.jsも起動させます。 | |
$ yarn dev |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ # srcフォルダにいることを確認してください。 | |
$ # babelプラグインをインストールします。 | |
$ yarn add -D babel-plugin-module-resolver babel-plugin-transform-runtime babel-preset-es2015 babel-preset-stage-0 | |
$ # nuxt.config.jsという設定ファイルを編集します。 | |
$ vi nuxt.config.js |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"rewrites": [ | |
{ | |
"source": "**", | |
"function": "nuxtApp" | |
} | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const functions = require('firebase-functions') | |
const express = require('express') | |
const { Nuxt } = require('nuxt') | |
const app = express() | |
const config = { | |
dev: false, | |
buildDir: 'nuxt', | |
build: { | |
publicPath: '/public/' | |
} | |
} | |
const nuxt = new Nuxt(config) | |
function handleRequest(req, res) { | |
res.set('Cache-Control', 'public, max-age=600, s-maxage=1200') | |
nuxt.renderRoute('/').then(result => { | |
res.send(result.html) | |
}).catch(e => { | |
res.send(e) | |
}) | |
} | |
app.get('*', handleRequest) | |
exports.nuxtApp = functions.https.onRequest(app) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<ul> | |
<li v-for="(fact, factIdx) in facts" :key="factIdx"> | |
{{ fact.text }} | |
</li> | |
</ul> | |
</template> | |
<script> | |
import axios from 'axios' | |
export default { | |
async asyncData() { | |
const res = await axios.get('https://nuxt-ssr.firebaseio.com/facts.json') | |
const facts = res.data | |
return { facts } | |
} | |
} | |
</script> | |
<style> | |
</style> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
buildDir: '../functions/nuxt', |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment