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でのサーバー、クライアント間データ共有について
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Hori Godai
July 12, 2019
Programming
990
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Nuxtでのサーバー、クライアント間データ共有について
Hori Godai
July 12, 2019
More Decks by Hori Godai
See All by Hori Godai
TypeScript Compiler APIを使って 型のユニットテストをブラウザーで動かす
steelydylan
3
280
エディター付きのReact開発環境を ブラウザーだけで実装した話
steelydylan
9
2k
HonoでReact・TypeScriptの実行環境をブラウザー上に作る
steelydylan
1
2.6k
複数ピンをまとめて表示するYahoo!地図用のJavaScriptライブラリをつくりま作りました
steelydylan
1
1.3k
next.jsを使ったuniversal React 入門
steelydylan
1
330
a-blog cmsの静的書き出し機能を使って、 自分のブログを100%静的にした話
steelydylan
0
440
MySQLの GEOMETRY 型とJavaScriptの Geolocation API の活用事例
steelydylan
1
530
アップルップルの新しいオープンソースの紹介
steelydylan
0
550
a-blog cms をよくするために 取り組んだ3つのこと
steelydylan
0
640
Other Decks in Programming
See All in Programming
Oxlintのカスタムルールの現況
syumai
6
1.1k
JavaDoc 再入門
nagise
1
370
The NotImplementedError Problem in Ruby
koic
1
840
作って学ぶ、 JSX (TSX) ランタイムの基本
syumai
7
1.6k
Strategic Design in the Frontend: Moduliths & Micro Frontends @DDDEurope
manfredsteyer
PRO
0
110
Observability in Practice:Grafana 與 Edge Device SRE 的那些事
blueswen
0
170
Composerを使ったサプライチェーン攻撃の様子を眺めてみる #phpstudy
o0h
PRO
2
250
Contextとはなにか
chiroruxx
1
330
C# and C++ Interoperability - cho-dotnetnew
harukasao
0
270
「なぜそう決めたのか」を残し続ける仕組み ― Notion AI カスタムエージェント × Slack連携による設計判断の自動記録 - NIKKEI Tech Talk #47
niftycorp
PRO
0
210
[2026年度第1回ORセミナー] 計画最適化ベンチャーと競技プログラミング人材
terryu16
0
270
依存関係から依存物へ―Dependencyという言葉の歴史をひも解く
j_lee
0
120
Featured
See All Featured
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
440
XXLCSS - How to scale CSS and keep your sanity
sugarenia
250
1.3M
Testing 201, or: Great Expectations
jmmastey
46
8.2k
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
62
44k
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.3k
Building a A Zero-Code AI SEO Workflow
portentint
PRO
0
600
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
310
[SF Ruby Conf 2025] Rails X
palkan
2
1.1k
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2.3k
Believing is Seeing
oripsolob
1
150
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.2k
Fireside Chat
paigeccino
42
4k
Transcript
Nuxtでのサーバー、クライアント間 データ共有について
None
はじめてNuxtを触った時の自分の悩み nuxtServerInit , fetch , serverPrefetch , asyncData 色々あるけどクライアントだけで動かしたいコード、サ ーバー側で処理してクライアントに渡したいコードがあ
る場合とかどのメソッドを使ったらいいの??
深みにはまる serverPrefetch うまく動かないぞ! fetch はこのバージョンでは動かないのか?? nuxtServerInit 動いてない??
こういうフロー図を発見
この際なのでNuxtのライフサイクルと データの受け渡し周りを整理して見ました
まずはじめに動くのが、nuxtServerInit Vuexのaction として記述する context.commit すればvuexに入りデータが永続化する!
nuxtServerInit ただし、アプリケーションのロード時以降はnuxt-link で画面遷移しても発火しない ページ全体に必要なStoreデータはここで取得(ログイン情報など)
次にmiddlewareが動く! Vuexのstore が取れるのでログイン状態に応じてのリ ダイレクト処理をしたりなど!
次にLayoutが動作!
serverPrefetch Nuxt 2.6以降から使える! layout やpage コンポーネントで利用可 サーバーサイドでのみ動作するがthis コンテキストも利用できるのが嬉しい! 個人的にはlayoutレベルで必要なデータを取得する際に利用 this.$store.dispatch
でデータをvuexに送れるのが嬉しい!! Promise をreturn しないとクライアントからは更新されたVuex の結果が見れない! Vue Vue. .extend extend( ({ { serverPrefetch serverPrefetch( () ) { { return return this this. .$store $store. .dispatch dispatch( ('user/fetch' 'user/fetch', , this this. .$ $ } } } }
fetch Nuxt 2.8以降から使える! pageコンポーネントで利用可 this が使えない。。。 個人的にはページレベルで必要なデータを取得する際に利用 Vuex にcommit したりできるぞ!
Promise をreturn しないとクライアントからは更新されたVuex の結果が見れない! Vue Vue. .extend extend( ({ { async async fetch fetch ( ({ { store store, , params params } }) ) { { const const res res = = await await axios axios. .get get( ('http://my-api/sta 'http://my-api/sta store store. .commit commit( ('setStars' 'setStars', , res res. .data data) ); ; } } } }) )
asyncData あらかじめサーバーサイド側でdata に格納したい値をセットできる! this が使えない。。。。 Vuex にdispatch したりはできなさそう。。。 Vue Vue.
.extend extend( ({ { asyncData asyncData ( (context context) ) { { return return { { project project: : 'nuxt' 'nuxt' } } } } } }) ); ;
apolloモジュールを使っている場合 @nuxtjs/apollo モジュールを使えばapollo から 取得した値をthis.data に格納できる。 prefetch:true にしておけばサーバーサイドで取得できる! this に依存するコードは書けない
asyncData に近いイメージ! Vue Vue. .extend extend( ({ { apollo apollo: : { { some some: : { { prefetch prefetch: : true true, , query query: : someGql someGql, , result result( ({ { data data } }) ) { { return return { { some some: : data data. .some some } }; ; } } } } } } } }) ); ;
まとめ nuxtServerInit , fetch , serverPrefetch , asyncData を理解して快適にサーバーからデータを受け取ろう!
ありがとうございました!
@steelydylan github twitter