SlideShare a Scribd company logo
1 of 18
Download to read offline
AWS Lambdaで
Vue.js SSRしてみた
2019/3/19
@coppieee
自己紹介
八木大介
Web: https://coppieee.com
twitter: @coppieee
フリーランスプログラマー
Webサイト/Webアプリの作成がメイン。
Vue.js, TypeScript, Serverlessなど。
Vue.jsでSSR(サーバーサイドレンダリング)したい!
ブログを作ろうと思ったときにやっぱり検索に引っかかってほしい。
動的にtitle書き換えとかOGPとかRSSとか対応していきたい。
きちんと404を返したい。
でもNuxtは使いたくない
・SSRだけのためにフレームワークを導入したくない
・Vue.jsのプレーンな状態がいい
理想としては最初はSSRせずに開発して、
必要になったらSSRを導入できるような環境を構築したい
Vue.js サーバサイドレンダリングガイド
https://ssr.vuejs.org/ja/
Vue.js公式でドキュメントが公開さ
れている。
vue-server-rendererというSSR用
のライブラリがある。
これ読めば作れる。
作ったサンプル
https://blog.coppieee.com
メモ帳ぽい感じのやつ。
(将来的にブログにする)
htmlを表示するとSSR出来ているのがわかる
構成
・Serverless Framework (API Gateway,AWS Lambda)
・S3 (静的ファイル置き場 js, css, png)
・CloudFront (S3とLambdaの割り振り)
・Amplify Framework (AppSync, GraphQL)
・Route 53 (ドメイン)
SSRしているのはAWS Lambda。
なぜAWS Lambdaか
・アクセスされた時のみ動くので、常にサーバを動かすよりコストが安い。
(個人で使う分には大抵無料枠で収まる)
・勝手にスケールしてくれる
いままで(フロントエンドのみ)
CloundFront
S3
Amplify &
AppSync
静的ファイル
js,css,img,...etc
GraphQL API
Client
処理の流れ
CloundFront
API Gateway &
Lambda
S3
Amplify &
AppSync
ページを表示する場合
(/ , /items/:id などのリクエスト)
SSRされたhtmlを返す
静的ファイル
js,css,img,...etc
GraphQL API
GraphQL API
(ページの更新のときにVue.jsから呼ばれる)
Client
clientとserver2つのエントリーポイント
最初はフロントエンドのみで普通にVue.jsで開発
必要になったら後付でSSRを有効化
といったことも可能になった。
なのでやっぱりSSRしたいといった場合に対応できるので、
こういった手法を覚えておいてもいいかも。
おわり
補足
entry-lambda.ts
import { createApp } from './app'
export const main: APIGatewayProxyHandler =
async (event, _context):Promise<APIGatewayProxyResult> => {
const context = {
title: 'coppieee blog',
url: event.path,
}
const { app, router, store } = createApp()
const html = await renderer.renderToString(app,context)
return {
statusCode:(context as any).HTTPStatus || 200,
headers:{'Content-Type':'text/html'},
body:html,
}
}
サーバコード
vue-server-rendererというライブラリで、
Vue appとcontextを元にhtmlを生成している。
左はエラー処理など端折ってるけど、
実際のコードも70行程度で済んでいる。
app.ts
import Vue from 'vue'
import App from './app-root.vue'
import { createRouter } from './router'
import {createStore} from './store'
export function createApp () {
const router = createRouter()
const store = createStore()
Vue.prototype.$store = store
const app = new Vue({
router,
render: h => h(App)
})
return { app, router ,store}
}
サーバ、クライアント共通のコード。
new Vue()してexportしてるだけ。
entry-client.ts
import { createApp } from './app'
const { app,store } = createApp()
app.$mount('#app')
クライアントコード。
app.$mount()してるだけ。
item.vue
<template>...省略</template>
<script lang="ts">
@Component({})
export default class Item extends Vue{
item:BlogItem = {id:'',title:'',description:''}
async fetchItem(){
const id = this.$route.params.id
const item = await this.$store.item({id})
if(item){ this.item = item}
}
async mounted(){ await this.fetchItem() } // クライアント側の処理
async serverPrefetch() { await this.fetchItem() } // サーバ側の処理
}
</script>
クライアント側の処理は mounted()でして、
サーバ側の処理はserverPrefetch()でする。
serverPrefetchはVue 2.6で追加されたもの。

More Related Content

Featured

Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationErica Santiago
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellSaba Software
 

Featured (20)

Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 

AWS LambdaでVue.js SSRしてみた