Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Nuxt(SPA)でもOGPしたい/want to render ogp with nuxt ...
Search
果物リン
May 27, 2019
Technology
4
3.3k
Nuxt(SPA)でもOGPしたい/want to render ogp with nuxt spa mode
ここにSPAモードのプロッジェクトがあります。
OGP?はい、1個設定できますね。
ページごとに設定したい?いったいなにをいっているんだ。
果物リン
May 27, 2019
Tweet
Share
More Decks by 果物リン
See All by 果物リン
Electron+Vue+Swift=真のデスクトップアプリ / How to create "Really" desktop app using Electron
fruitriin
0
16
ある日オレオレフレームワークを作りたくなったぞ/Want to Create Oreore Framework
fruitriin
1
76
fishとfzfはじめて 豊かになりました / fish and fzf is good
fruitriin
1
700
Chrome Devtools使いこなしたい/I want to master devtool
fruitriin
3
1.4k
Event Stage - 真面目に作ったけど 全くウケなくてクソだったアプリ/I made new service but they say shit
fruitriin
0
150
読み上げチャット+映像配信サービス作ってるけど…/I develop Text-to-Speach chat and broadcast Savice but...
fruitriin
0
97
とある自社開発の苦楽の軌跡/Trajectory of a Certain In-house Development Effort
fruitriin
1
220
動画再生速度を変更するコンポーネントを作ろうとして諦めた話/talk about give up to create video with playback-rate controller
fruitriin
6
860
Soft Skillsから伝えたい新人時代の個人開発技術選定/Selecting Stack for Personal Development as a from Soft Skills
fruitriin
2
340
Other Decks in Technology
See All in Technology
俺とVSCode Python Debugger Extension
sat
PRO
1
150
最新のWasm事情
askua
5
2.5k
さくっと実践!Postmanを活用した高品質で持続可能なAPI管理
yokawasa
5
360
AWS Lambda と Amazon SQS で「わかった気になれる」FreeRTOS 入門
soracom
PRO
2
150
0x5F3759DF
ykozw
0
340
XSS攻撃から考察するAWS設定不備の恐怖/20241012 Hironobu Otaki
shift_evolve
0
150
ラブグラフ紹介資料 〜プロダクト解体新書〜 / Lovegraph Product Deck
lovegraph
0
14k
なぜ Rack を理解すべきかプレトーク / Why should you understand Rack - Pre-talk
hogelog
0
230
テクニカルライターのチームで「目標」をどう決めたか / MVV for a Team of Technical Writers
lycorptech_jp
PRO
3
160
Deep dive into Nuxt Server Components
wattanx
1
1.3k
VPoE Meetup Vol.1 VPoEとして実践してきたことと反省点
coconala_engineer
2
180
RDS for Db2 データ移行編 - Part2:S3経由のバックアップ・リストアでデータ移行 /20241011-RDSforDb2-dojo
mayumihirano
0
140
Featured
See All Featured
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
106
48k
How to train your dragon (web standard)
notwaldorf
88
5.6k
No one is an island. Learnings from fostering a developers community.
thoeni
19
2.9k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.6k
Bash Introduction
62gerente
608
210k
Become a Pro
speakerdeck
PRO
24
4.9k
Gamification - CAS2011
davidbonilla
80
5k
Building an army of robots
kneath
302
42k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Building Adaptive Systems
keathley
38
2.2k
Side Projects
sachag
452
42k
Art, The Web, and Tiny UX
lynnandtonic
296
20k
Transcript
Nuxt(SPA)でも OGPしたい ՌϦϯ@FruitRiin גࣜձࣾΏΊΈ Roppongi.vue #1 2019/05/27
Q. NuxtͰ ϖʔδݸผʹOGPΛ ઃఆ͍ͨ͠Ͱ͢
SSR, UniversalϞʔυ headͰϝλλάઃఆ
Yes! ͦ͏Ͱ͋Δ͖ʂ
ͱ͜ΖͰ ͜͜ʹSPAϞʔυͷ ϓϩδΣΫτ͕͋Γ·͢
ʮͳΜͰSPAϞʔυͳΜ͔͢ʁʯ ʮ༷͔ͩΒɺܦҢΘ͔ΒΜʯ ʮ͍͞Ͱʯ
ΫϥΠΞϯτͷσΟϨ ʮOGPग़ͳ͍ΜͰ͔͢ʁʯ
Ͱ͢ΑͶʔ
͙͵͵͵…… SPA͔ΒUniversalϞʔυͷΓସ͑…… →fetchͰͷwindow objectૢ࡞͕શ໓ Ұ෦ઃܭΓͳ͓͠ ݕূίετܹ૿……
ΫϥΠΞϯτͷΤϯδχΞ ʮNuxtʹ͢લʹExpressͰ HTMLΛվ͟Μ͢Ε ͍͚ΔͷͰʁʯ
ΓɺΓͭ͘Θ͔Δ Θ͔Δ͕……
Nuxt(SPA)でも OGPしたい 闇の魔術に対する防衛術
自己紹介 • ՌϦϯ@FruitRiin • גࣜձࣾΏΊΈ • ·ͳ͘ೖࣾ4ϲ݄ • ϑϩϯτΤϯδχΞɺνʔϜϦʔμʔɺPMʁ •
Vue͍͍ͧʂ
ʮ͍ͬͯ͏ͳΜ͚ͩͲ͞ɺ Ͱ͖Δͱࢥ͏ʁʯ ʮͪΐͬͱௐͯΈ·͠ΐ͏͔ʯ
ʮͰ͖ͦ͏ͳؾ͕͠·͢ʁʯ ʮϚδͰʁʯ
方針:nuxt startしない プロジェクトを目指す • nuxt start͢Δͱαʔόʔ͕ىಈ͢Δ • վ͟Μͷ༨ͳ͠ • expressΛىಈ͢Δapp.js
తͳͷΛͭ͘Δ • nuxt start ͷΘΓʹ node app.js ͢Δ
app.js? • NodeͰಈ͔͢ϓϩάϥϜͱͯ͠ࢦఆ͢ΔϑΝΠϧ • ߏཁ݅ • nuxtΛexpressͷϞδϡʔϧͱͯ͠ॳظԽ • ։ൃϞʔυͷͱ͖ϗοτϦϩʔυ༻ʹϦϏϧυ •
expressʹϦΫΤετ͕དྷͨͱ͖ Nuxt.jsͰϨϯμϦϯά • expressΛࢦఆϙʔτͰػͤ͞Δ
ͳΔ΄Ͳ·ͬͨ͘Θ͔ΒΜ
app.js • ࢀߟจݙ: nuxt.render() https://ja.nuxtjs.org/api/nuxt-render/ • جຊతͳܕͱ্ͯ͠هͷίʔυΛ ͦͷ··ར༻͠·͠ΐ͏
const { Nuxt, Builder } = require('nuxt') const app =
require('express')() const isProd = (process.env.NODE_ENV === 'production') const port = process.env.PORT || 3000 // Nuxt.js ΛΦϓγϣϯͱͱʹΠϯελϯεԽ͢Δ const config = require('./nuxt.config.js') config.dev = !isProd const nuxt = new Nuxt(config) // ͯ͢ͷϧʔτΛ Nuxt.js ͰϨϯμϦϯά͢Δ app.use(nuxt.render) // ϗοτϦϩʔσΟϯά͢Δ։ൃϞʔυͷͱ͖ͷΈϏϧυ͢Δ if (config.dev) { new Builder(nuxt).build() .then(listen) } else { listen() } function listen() { // αʔόʔΛ Listen ͢Δ app.listen(port, '0.0.0.0') console.log('Server listening on `localhost:' + port + '`.') }
ͦ͏͍͏ͷͩͱఘΊͯ͘Ε
備考:create-nuxt-app • Use a custom server framework? ʹ -> express
ͱճ͢Δ • server/index.js Λࣗಈੜͯ͘͠ΕΔʂʂ • ༰ઌఔͷϑΝΠϧͱ֓Ͷಉ͡
HTMLを取得したい • HTMLͷऔಘʹ nuxt.renderRoute() Λ͏ • SPAϞʔυͰ࣮࣭ςϯϓϨʔτ͕औಘͰ͖Δ • ฦΓͷΦϒδΣΫτʹؚ·ΕΔ html
ʹ ϨϯμϦϯά͠Α͏ͱͨ͠HTML͕ೖ͍ͬͯΔ
ಛఆϧʔςΟϯάͷͱ͖ͷΈͷॲཧ • ϧʔςΟϯάͰॲཧΛ͚͍ͨˠapp.get() ͳͲΛ͏ • express ͷgetϝιου • app.get(‘/detail’) ,
app.get(‘/user/:userId’) ͳͲ
const { Nuxt, Builder } = require('nuxt') const app =
require('express')() const isProd = (process.env.NODE_ENV === 'production') const port = process.env.PORT || 3000 // Nuxt.js ΛΦϓγϣϯͱͱʹΠϯελϯεԽ͢Δ const config = require('./nuxt.config.js') config.dev = !isProd const nuxt = new Nuxt(config) // ͯ͢ͷϧʔτΛ Nuxt.js ͰϨϯμϦϯά͢Δ app.use(nuxt.render) // ϗοτϦϩʔσΟϯά͢Δ։ൃϞʔυͷͱ͖ͷΈϏϧυ͢Δ if (config.dev) { new Builder(nuxt).build() .then(listen) } else { listen() } function listen() { // αʔόʔΛ Listen ͢Δ app.listen(port, '0.0.0.0') console.log('Server listening on `localhost:' + port + '`.') } େࣄͦ͏
// Nuxt.js ΛΦϓγϣϯͱͱʹΠϯελϯεԽ͢Δ const config = require('./nuxt.config.js') config.dev = !isProd
const nuxt = new Nuxt(config) // detailͰಛผॲཧ app.get(‘/detail', async (req, res) => { const result = await nuxt.renderRoute('/'); console.log(result.html) }) // ͯ͢ͷϧʔτΛ Nuxt.js ͰϨϯμϦϯά͢Δ app.use(nuxt.render) Ճ͠·ͨ͠ ↑Ͱॲཧ͞Εͳ͚Ε ɹͬͪ͜Ͱॲཧ͢Δ
None
axiosで必要なデータを引っ張る • ͜͜ීஈͱ͋·ΓมΘΒͳ͍ • axios.get() ͳΓͳΜͳΓ
改ざんしてレンダリング • Կߟ͑ͣʹےͰ String.replace() • String.replace(/<meta data-hid=“og:title”.*?>/, 'ʢུʣ') • ஔͨ͠ͷΛ
res.send() ͢Δ
// detailͰಛผॲཧ app.get("/detail", async (req, res) => { const result
= await nuxt.renderRoute("/"); let html = result.html; const meta = await axios.get("/someting-meta") html = html.replace(/<meta data-n-head="true" data-hid=“og:title".*?>/, `<meta title="og:title" content="${meta.title}">`) res.send(html) }); Ճ͠·ͨ͠
None
None
ΊͰͨ͠ɹΊͰͨ͠
メリット • ҠߦʹൺΕݕূίετ͕͍ܰ • طଘͷSPAͷॲཧΛݟ͞ͳͯ͘Α͍ • αʔόʔͰDOMͷܭࢉΛ͠ͳ͍ͰΑ͍ • expressΛىಈ͢Δඞཁ͋ΔͷͰɺ αʔόʔϦιʔεΛ৯Θͳ͍Θ͚Ͱͳ͍
デメリット • UniversalϞʔυͰ͍͍ͳΒ UniversalϞʔυͷ΄͏͕Α͍ • Expressͷ͕ࣝଟগඞཁ • Δ͜ͱ͕టष͍
デメリット2 • ϖʔδίϯϙʔωϯτͷϧʔςΟϯάͱ վ͟ΜॲཧͷϧʔςΟϯά͕Ұக͠ͳ͍ • NuxtͷࣗಈϧʔςΟϯάͷԸܙΛड͚ΒΕͳ͍ • ̍ϖʔδͷॲཧ͕2Օॴʹ͔ΕΔ • ରͷϖʔδ͕૿͑Δͱίʔυ͕ͨΒ͘ͳΔ
͍ͬͯ͏͔ UniversalϞʔυͰ͖ΔͳΒ ຊʹͦͷํ͕͍͍
ཁ݅ʹ߹Θͤͯ SPAϞʔυͰ։ൃΛਐΊ͍͍ͯͷ͔ ࠓҰߟ͑Α͏
SPAϞʔυ͡Ό OGPग़ͳ͍ͧͬʂ