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.jsのComponent分割単位と,SSRをする上で気をつけること
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
omiend
January 26, 2019
Programming
810
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Nuxt.jsのComponent分割単位と,SSRをする上で気をつけること
omiend
January 26, 2019
More Decks by omiend
See All by omiend
スクラッチ組織活用術 Salesforce Developers Meetup #24
omiend
0
1.4k
Nuxt.js_Vue.js_のVuexの動かしかたをまとめてみたよ.pdf
omiend
0
120
20181208_好きだけじゃツライScala.js_共有版_.pdf
omiend
0
130
20181110 Scala関西Summit 懇親会LT「Scalaのかわいいところ」
omiend
0
590
Other Decks in Programming
See All in Programming
freeeにおけるEvalsの実践例の紹介
freee
PRO
0
110
PHP Application における Kubernetes 内 gRPC 通信
ganchiku
0
590
Google Apps Script で Ruby を動かす
kawahara
0
220
<title><a id="</title>君はこのHTMLをパースできるか"></a></title> #雑LT_study
pizzacat83
0
140
琵琶湖の水は止められてもNet--HTTPのリトライは止められない / You might be able to stop the water flow of Lake Biwa but you can't stop Net::HTTP retries
luccafort
PRO
0
690
S3 を使うアプリケーションをローカル完結で動かすことに全力を注いでみた / Running S3 Apps Offline
contour_gara
0
470
GDG Korea Android: 2026 I/O Extended ~ What's new in Android development tools
pluu
0
220
the container ship “Apple Silicon”@WWDC26 Recap -Japan-\(region).swift
shingangan
0
120
進化を続けるGo toolsの現在地 / The Current State of Ever-Evolving Go Tools
hond0413
0
210
そこに3びきプロダクトがいるじゃろう——生成AI時代における“価値が届かない理由”の構造
kosuket
0
480
わからない話を追いかけたら、プログラミング言語を作る側にいた
ydah
3
500
「人を評価する AI」の設計と実装
ryoyanara
0
200
Featured
See All Featured
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
460
Deep Space Network (abreviated)
tonyrice
0
250
Designing for Performance
lara
611
70k
Designing for Timeless Needs
cassininazir
1
440
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
16k
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.5k
The Curious Case for Waylosing
cassininazir
1
450
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.5k
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
460
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
540
Designing Experiences People Love
moore
143
24k
Transcript
Nuxt.jsのComponentについて @omiend Store
自己紹介 @omiend 恵比寿で働くプログラマー歴13年目のおじさん 一応フロントエンドからバックエンドまで一通り出来ると思い込んでいるダメェンジニア 時々パン売ってます。(次回予定3月横浜赤レンガ/4月新宿たかしまや) PeopleSoft,Java,Shell,MySQL,SQL,Salesforce(Force.com/ApexCode/VisualCode),GAE(Slim3),ABAP Scala(ScalaMatsuri Staff),Ruby on Rails,Docker,Angular(1.4…),React/Redux,Riot.js,Vue.js,Nuxt.js
Musics,Movies,Joging,Walking,Drink Driven Development
話すこと - Nuxt.js のComponent分割単位 - Nuxt.js SSRする時に気をつけること @omiend 今日も一日
@omiend Nuxt.jsのComponent分割単位
@omiend Nuxt.js は Vue アプリケーションを作成するフレームワークです。 ユニバーサルアプリケーション、静的に生成されるアプリケーショ ン、シングルページアプリケーションの中から作成するアプリケー ションを選ぶことができます。 はじめに -
Nuxt.js https://ja.nuxtjs.org/guide/ Nuxt.jsとは
Nuxtは - Layouts - Pages - Components という単位でページを構成する。 @omiend Nuxt.jsのページ定義
layouts/default.vue pages/index.vue components/TheFooter.vue components/TheHeader.vue components/SomeSection.vue components/ UserCard.vue components/ TheSideNavi.vue components/ UserCard.vue
@omiend 右ののようなページを作成する場合、下記のようなディレクトリ構 造となる。 . ├── components │ ├── TheSideNavi.vue │
├── TheFooter.vue │ ├── TheHeader.vue │ ├── SomeSection.vue │ └── UserCard.vue ├── layouts │ └── default.vue └── pages └── index.vue Nuxt.jsのページ定義 layouts/default.vue pages/index.vue components/TheFooter.vue components/TheHeader.vue components/SomeSection.vue components/ UserCard.vue components/ TheSideNavi.vue components/ UserCard.vue
Layoutsは全ページ共通のレイアウトを定 義するソースファイル。 Nuxtがデフォルトで用意するdefault.vue とは別のソースファイルを定義し、ページご とに適用を分けることもできる。 @omiend Layouts layouts/default.vue pages/index.vue components/TheFooter.vue
components/TheHeader.vue components/SomeSection.vue components/ UserCard.vue components/ TheSideNavi.vue components/ UserCard.vue
Pagesは各ページ(トップページやアバウト ページなど)を定義するソースファイル。 定義したディレクトリによりNuxtがRouting を自動生成してくれる。 @omiend Pages layouts/default.vue pages/index.vue components/TheFooter.vue components/TheHeader.vue
components/SomeSection.vue components/ UserCard.vue components/ TheSideNavi.vue components/ UserCard.vue
Componentsでは、再利用性の高い部品を 定義する。 例では、HeaderやFooter、または GlobalNaviなどを定義してLayoutで importしたり、ユーザ1人1人の情報を表 示するカードを定義している。 @omiend Components layouts/default.vue pages/index.vue
components/TheFooter.vue components/TheHeader.vue components/SomeSection.vue components/ UserCard.vue components/ TheSideNavi.vue components/ UserCard.vue
このComponentsはHTMLの要素としてど の単位にも分割・定義をすることができる。 しかし、設計・再現無く分割・定義しまくると カオス化することが想定される。 では、Componentsはどの粒度で分割して 定義するのが良いか? @omiend Componentsの分割単位 layouts/default.vue pages/index.vue
components/TheFooter.vue components/TheHeader.vue components/SomeSection.vue components/ UserCard.vue components/ TheSideNavi.vue components/ UserCard.vue
ざっくりまとめると、弊社では - (HTML5の)Sectionタグ - 部品(Cardなど) の粒度で分割・定義することとした。 Blogに書きました: #Nuxt.jsのComponentをどの単位で分けるか悩んだ話 https://omiend.hatenablog.jp/entry/2018/12/27/203359 @omiend
layouts/default.vue pages/index.vue components/TheFooter.vue components/TheHeader.vue components/SomeSection.vue components/ UserCard.vue components/ TheSideNavi.vue components/ UserCard.vue Componentsの分割単位
@omiend その上で、書きのディレクトリ構造を少し変更して・・・ . ├── components │ ├── TheSideNavi.vue │ ├──
TheFooter.vue │ ├── TheHeader.vue │ ├── SomeSection.vue │ └── UserCard.vue ├── layouts │ └── default.vue └── pages └── index.vue Componentsの分割単位 layouts/default.vue pages/index.vue components/TheFooter.vue components/TheHeader.vue components/SomeSection.vue components/ UserCard.vue components/ TheSideNavi.vue components/ UserCard.vue
このような感じにしました。 . ├── components │ ├── parts │ │ └──
UserCard.vue │ └── sections │ ├── TheSideNavi.vue │ ├── TheFooter.vue │ └── TheHeader.vue ├── layouts │ └── default.vue └── pages └── index.vue @omiend Componentsの分割単位 layouts/default.vue pages/index.vue components/TheFooter.vue components/sections/TheHeader.vue components/sections/ SomeSection.vue components/ parts/ Card.vue components/ sections/ TheSideNavi.vue components/ parts/ Card.vue
異論は認める。 @omiend Componentsの分割単位 layouts/default.vue pages/index.vue components/TheFooter.vue components/sections/TheHeader.vue components/sections/ SomeSection.vue components/
parts/ Card.vue components/ sections/ TheSideNavi.vue components/ parts/ Card.vue
※SSR = Server Side Rendering SPAだと当該ページにアクセスしてから DOM(仮想DOM)を構築してページを構成するので、クローラーが巡回してきた時は「なにも無いページ」と解釈されてしまう。 そこでSSRにて対応する。 ※SPA =
Single Page Application @omiend Nuxt.jsにおけるSSRのしかた
@omiend 日記はここで終わっている・・・ Nuxt.jsにおけるSSRのしかた
TL;DR - Componentsで利用するデータや機能は、 Components内部で完結させたい - Nuxt.jsでSSRをするときは、asyncData()とfetch()を利用する - ComponentsからはasyncData()を利用することが出来ない( Nuxt.jsの仕様) -
asyncData()はPagesで利用する必要があり、 Componentsにデータを渡す場合は Propsを利用する - Propsは利用したくない - Vuexを利用すれば良い - asyncData()からはVuexのStoreにアクセスできない - fetch()であれば、Storeにアクセスできる まとめ - SSRをしたい - Componentsにデータを渡すか? - Yes - Pagesにfetch()を定義し、Storeにデータを格納 - ComponentsからはStoreのデータを参照する - No - PagesにasyncData()を定義し、Pagesのdata要素とマージさせる @omiend Nuxt.jsにおけるSSRのしかた
ソース https://github.com/omiend/omiend-my-api アプリ(firebase) https://omiend-my-api.firebaseapp.com/ssr @omiend Nuxt.jsにおけるSSRのしかた
@omiend ご清聴 ありがとうございました Store