Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Nuxtでのサーバー、クライアント間データ共有について
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
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
Transcript
17 slides
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
More Decks by Hori Godai
See All by Hori Godai
TypeScript Compiler APIを使って 型のユニットテストをブラウザーで動かす
steelydylan
3
310
エディター付きのReact開発環境を ブラウザーだけで実装した話
steelydylan
9
2.1k
HonoでReact・TypeScriptの実行環境をブラウザー上に作る
steelydylan
1
2.8k
複数ピンをまとめて表示するYahoo!地図用のJavaScriptライブラリをつくりま作りました
steelydylan
1
1.4k
next.jsを使ったuniversal React 入門
steelydylan
1
350
a-blog cmsの静的書き出し機能を使って、 自分のブログを100%静的にした話
steelydylan
0
460
MySQLの GEOMETRY 型とJavaScriptの Geolocation API の活用事例
steelydylan
1
540
アップルップルの新しいオープンソースの紹介
steelydylan
0
590
a-blog cms をよくするために 取り組んだ3つのこと
steelydylan
0
670
Other Decks in Programming
See All in Programming
はじめてのPLATEAU
nokonoko1203
1
670
Materialが消えた日 〜真っ白なFlutterキャンバスにRenderObjectで絵を描く〜
umigishiaoi
0
170
エージェントの管理もエージェントに – herdr-core と自作バックログで作る司令塔 –
nearme_tech
PRO
1
110
SODA - FACT BOOK(EN)
sodainc
1
280
Debian JP Projectのサーバー構成
uwabami
1
180
protojson における文字化けの扱い
kazuki1023
0
460
自宅サーバはどこまで再現できるのか - Proxmox VE上のVM環境をIaCとバックアップで戻してみた
youto
0
450
ローカルLLMの基本と近況 2611 / Local LLM basics and recent situation 2611
kishida
3
660
小さなアプリを作って公開するまで
onozaty
0
240
AI駆動TDD ― 2026年10月版
hrmstrsmgs
0
110
テストだけで見つかる? Rubyでつくるメソッド発掘器
izawa
0
160
baseline으로 마이그레이션, 리팩토링 안전하게 맡기기
fornewid
1
1.1k
Featured
See All Featured
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
1.1k
AI: The stuff that nobody shows you
jnunemaker
PRO
12
1.3k
Product Roadmaps are Hard
iamctodd
55
13k
Design in an AI World
tapps
1
400
The Limits of Empathy - UXLibs8
cassininazir
2
800
Between Models and Reality
mayunak
4
610
Discover your Explorer Soul
emna__ayadi
2
1.3k
How to Ace a Technical Interview
jacobian
280
24k
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
1
610
The Invisible Side of Design
smashingmag
301
52k
The browser strikes back
jonoalderson
0
2k
svc-hook: hooking system calls on ARM64 by binary rewriting
retrage
2
690