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
Vueと比べて理解するNuxtの機能~auto-import編~
Search
IIHARA
October 17, 2023
Technology
120
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Vueと比べて理解するNuxtの機能~auto-import編~
IIHARA
October 17, 2023
More Decks by IIHARA
See All by IIHARA
Vue3+Firebase Auth環境で苦労した話
gityosan
0
220
Docusで知り合い向け学習サイト作ってみた
gityosan
0
84
新卒エンジニアが週一でいろんなLTに参加・登壇してみた話
gityosan
1
190
Nuxt3にStorybookを正しく入れてみた
gityosan
0
850
Marpをカスタマイズして爆速スライド開発環境を手に入れよう
gityosan
0
720
TiptapでストレスフリーなWYSIWYGエディター開発を!
gityosan
0
500
Other Decks in Technology
See All in Technology
AI駆動開発で仕様はどこまで書くべきか? ― 人とAIの責務境界から考える開発プロセスの実践
takahiromatsui
1
130
Lambda MicroVMsは常駐サーバーの代わりに なるか? Kiro Crew を動かして検証してみた / Kiro Crew on Lambda MicroVMs
k_adachi_01
2
300
Goodbye ShellScript, Hello File-based App
shunsock
0
1.3k
音声コミュニティを守るAI監視基盤_ 90%以上の入力削減を支えたServerless設計と運用判断
shuheioka123
0
110
[Kiro Meetup #7] Kiro Crew Dive Deep
konippi
0
400
【Findyテック文化祭ワークショップ】新卒エンジニア&採用担当と作る、 なりたい姿と今やるべき一歩
dip_tech
PRO
0
130
【ゲームメーカーズスクランブル2026】『Shadowverse: Worlds Beyond』UIとアニメーションで実現する最高のユーザー体験を叶えるプロトタイピング
cygames
PRO
1
680
人にやさしく、AIにやさしく、書き手を選ばないIaCのガードレール再考 / Rethinking IaC Guardrails for Humans and AI Alike
kohbis
5
1.6k
手を動かして実感する、Kiro が変える開発体験
inariku
0
400
AIに書かせて、プラットフォームで縛る ― EKSプラットフォームで実践した責任境界と権限設計
elmodev09
1
920
AIは推し活である。
kurazuuuuuu
2
1k
IR Today: Theory, Practice, and Agents
dtunkelang
0
180
Featured
See All Featured
The World Runs on Bad Software
bkeepers
PRO
72
12k
[Rails World 2026] Durable orchestration on Rails: from continuation to workflow
palkan
1
360
The Mindset for Success: Future Career Progression
greggifford
PRO
0
520
Building a A Zero-Code AI SEO Workflow
portentint
PRO
0
750
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
3
270
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
600
Faster Mobile Websites
deanohume
310
32k
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
490
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
340
Done Done
chrislema
187
17k
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2.5k
Transcript
Vueと比べて理解するNuxtの機能 auto-import編 1
目次 1. 自己紹介 2. きっかけ 3. vueで確認する 4. Nuxtで確認する 5.
ソースコードで確認する Engineer LT Night #1 @渋谷 2
自己紹介
飯原帆隆 株式会社メタップスホールディングス エンジニア Vue/Nuxt Rails Litを主に書きます Github: gityosan Engineer LT
Night #1 @渋谷 4
きっかけ
きっかけ vue3で開発していて、Nuxt3と比べてルーティングや各種関数の初期化や読み込 み周りで辛さを感じることがあったため、しっかりと調査して比較することにしま した。 Engineer LT Night #1 @渋谷 6
まず、vueで確認してみる
ログを埋め込む // main.ts const resolveFunc = () => { return
new Promise((resolve) => { setTimeout(() => { resolve('main.ts:resolveFunc:resolved') }, 2000) }) } const asyncCall = async () => { console.debug('main.ts:asyncCall:calling') const result = await resolveFunc() console.debug(result) } new Promise((resolve) => { setTimeout(() => { resolve('main.ts:Promise:resolved') }, 2000) }).then(() => { console.debug('main.ts:Promise:then') }) asyncCall().then(() => { console.debug('main.ts:asyncCall:then') }) // router/middleware.ts const resolveFunc = () => { return new Promise((resolve) => { setTimeout(() => { resolve('beforeEach:resolveFunc:resolved') }, 2000) }) } const asyncCall = async () => { console.debug('beforeEach:asyncCall:calling') const result = await resolveFunc() console.debug(result) } new Promise((resolve) => { setTimeout(() => { resolve('beforeEach:Promise:resolved') }, 2000) }).then(() => { console.debug('beforeEach:Promise:then') }) asyncCall().then(() => { console.debug('beforeEach:asyncCall:then') }) Engineer LT Night #1 @渋谷 8
トップレベルに一切awaitを付けなかった場合 Engineer LT Night #1 @渋谷 9
トップレベルにawaitを付けた場合 Engineer LT Night #1 @渋谷 10
下記のように関連する処理全てにawaitを付けた としても、、、 // main.ts const app = await createApp(AppEmployee) await
app.use(router) await app.component() await app.provide(storeKey, createGlobalStore()) await app.mount('#app') // router.ts - router.beforeEach(authCheckOnRouteChanged) + router.beforeEach(async (to, from) => { + await authCheckOnRouteChanged(to, from) + }) main.tsとrouter.tsの処理が非同期的に進みなが らapp.vueのmountへとつながっている Engineer LT Night #1 @渋谷 11
一方、Nuxt
ログを埋め込む // plugin/index.ts const resolveFunc = () => { return
new Promise((resolve) => { setTimeout(() => { resolve('plugin:resolveFunc:resolved') }, 2000) }) } const asyncCall = async () => { console.debug('plugin:asyncCall:calling') const result = await resolveFunc() console.debug(result) } export default defineNuxtPlugin(async (app) => { await new Promise((resolve) => { setTimeout(() => { resolve('plugin:Promise:resolved') }, 2000) }).then(() => { console.debug('plugin:Promise:then') }) await asyncCall().then(() => { console.debug('plugin:asyncCall:then') }) }) // middleware/index.ts const resolveFunc = () => { return new Promise((resolve) => { setTimeout(() => { resolve('middleware:resolveFunc:resolved') }, 2000) }) } const asyncCall = async () => { console.debug('middleware:asyncCall:calling') const result = await resolveFunc() console.debug(result) } export default defineNuxtRouteMiddleware(async (to) => { await new Promise((resolve) => { setTimeout(() => { resolve('middleware:Promise:resolved') }, 2000) }).then(() => { console.debug('middleware:Promise:then') }) await asyncCall().then(() => { console.debug('middleware:asyncCall:then') }) }) Engineer LT Night #1 @渋谷 13
トップレベルに一切awaitを付けなかった場合 Engineer LT Night #1 @渋谷 14
トップレベルにawaitを付けた場合 Engineer LT Night #1 @渋谷 15
Nuxtではasync awaitを適切に設定した場合、pluginとmiddlewareの読み込みが同 期的に進みます これが地味に嬉しい。 しかし、なぜこんな挙動をするのか Engineer LT Night #1 @渋谷
16
ソースコードで確認してみる
// https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/app/plugins/router.ts export default defineNuxtPlugin<{ route: Route, router: Router }>({
name: 'nuxt:router', enforce: 'pre', setup (nuxtApp) { // < 中略> async function handleNavigation (url: string | Partial<Route>, replace?: boolean): Promise<void> { try { // Resolve route const to = getRouteFromPath(url) // Run beforeEach hooks for (const middleware of hooks['navigate:before']) { const result = await middleware(to, route) // Cancel navigation if (result === false || result instanceof Error) { return } // Redirect if (typeof result === 'string' && result.length) { return handleNavigation(result, true) } } for (const handler of hooks['resolve:before']) { await handler(to, route) } // Perform navigation Object.assign(route, to) if (import.meta.client) { window.history[replace ? 'replaceState' : 'pushState']({}, '', joinURL(baseURL, to.fullPath)) if (!nuxtApp.isHydrating) { // Clear any existing errors await nuxtApp.runWithContext(clearError) } } // Run afterEach hooks for (const middleware of hooks['navigate:after']) { await middleware(to, route) } } catch (err) { if (import.meta.dev && !hooks.error.length) { console.warn('No error handlers registered to handle middleware errors. You can register an error handler with `router.onError()`', err) } for (const handler of hooks.error) { await handler(err) } } } // < 中略> } }) Engineer LT Night #1 @渋谷 18
// https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/app/entry.ts ... entry = async function initApp () {
if (vueAppPromise) { return vueAppPromise } const isSSR = Boolean( window.__NUXT__?.serverRendered || document.getElementById('__NUXT_DATA__')?.dataset.ssr === 'true' ) const vueApp = isSSR ? createSSRApp(RootComponent) : createApp(RootComponent) const nuxt = createNuxtApp({ vueApp }) try { await applyPlugins(nuxt, plugins) } catch (err) { await nuxt.callHook('app:error', err) nuxt.payload.error = (nuxt.payload.error || err) as any } try { await nuxt.hooks.callHook('app:created', vueApp) await nuxt.hooks.callHook('app:beforeMount', vueApp) vueApp.mount(vueAppRootContainer) await nuxt.hooks.callHook('app:mounted', vueApp) await nextTick() } catch (err) { await nuxt.callHook('app:error', err) nuxt.payload.error = (nuxt.payload.error || err) as any } return vueApp } ... Engineer LT Night #1 @渋谷 19
恐らく、、、 pluginはもとよりmiddlewareもrouterのpluginとして定義して、その上で初期化時にpluginを順次読み 込んでいるため先述のような挙動になるのだろうと考えられる Engineer LT Night #1 @渋谷 20
None
None
Thank you for listening!!