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
Hori Godai
July 12, 2019
Programming
1k
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
290
エディター付きのReact開発環境を ブラウザーだけで実装した話
steelydylan
9
2k
HonoでReact・TypeScriptの実行環境をブラウザー上に作る
steelydylan
1
2.6k
複数ピンをまとめて表示するYahoo!地図用のJavaScriptライブラリをつくりま作りました
steelydylan
1
1.3k
next.jsを使ったuniversal React 入門
steelydylan
1
340
a-blog cmsの静的書き出し機能を使って、 自分のブログを100%静的にした話
steelydylan
0
450
MySQLの GEOMETRY 型とJavaScriptの Geolocation API の活用事例
steelydylan
1
530
アップルップルの新しいオープンソースの紹介
steelydylan
0
560
a-blog cms をよくするために 取り組んだ3つのこと
steelydylan
0
650
Other Decks in Programming
See All in Programming
「寝てても仕事が進む」Claude Codeで組む第二の脳
tomoyafujita2016
0
280
仕様駆動開発の消費期限
watany
7
2k
Welcome to the "Parametricity" 🏙️ − Generic だけど Specific な世界 −
guvalif
PRO
1
200
使用 Meilisearch 建立新聞搜尋工具
johnroyer
0
220
Japan Community Day at Kubecon + CloudNativeCon Japan 2026: Learning Container Privilege Control by Building My Own Low-Level Container Runtime
ternbusty
1
130
Google Apps Script で Ruby を動かす
kawahara
0
130
Apache Hive: Toward a Cloud Native Lakehouse
okumin
0
180
なぜ関数型プログラミングで「型」と「証明」が語られるのか #fp_matsuri
kajitack
3
1.1k
全PRの83%がAIレビューだけでマージできるようになった開発組織はその後どうなったか
athug
1
1.4k
5分で問診!Composer セキュリティ健康診断
codmoninc
0
860
<title><a id="</title>君はこのHTMLをパースできるか"></a></title> #雑LT_study
pizzacat83
0
130
言語を使う側から、作る側へ。 自作 Lisp で得た新たな気づき。
andpad
0
150
Featured
See All Featured
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
690
Crafting Experiences
bethany
1
240
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
400
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
330
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
480
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.8k
A better future with KSS
kneath
240
18k
Measuring & Analyzing Core Web Vitals
bluesmoon
9
950
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.7k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.5k
The Invisible Side of Design
smashingmag
301
52k
Skip the Path - Find Your Career Trail
mkilby
1
180
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