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
Vueと比べて理解するNuxtの機能~auto-import編~
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
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
840
Marpをカスタマイズして爆速スライド開発環境を手に入れよう
gityosan
0
720
TiptapでストレスフリーなWYSIWYGエディター開発を!
gityosan
0
500
Other Decks in Technology
See All in Technology
GuardDuty 検知対応を DevOps Agent で効率化しようとしている話 / GuardDuty Investigations with DevOps Agent
masahirokawahara
1
230
[RSJ26] Building a VLA Model Based on Self-Distilled Classification
keio_smilab
PRO
0
180
書籍『生成AIの安全性入門』の入門
wataoka
0
190
Sony-DroidKaigi2026
sony
1
300
Guerilla InnerSource in enterprises, during the AI hype
onenashev
PRO
0
140
Instana&Bob トラブルシュートハンズオン
mayamasaki68
0
110
指示待ちから変化に応じるClaude Codeへ!~環境からAgentへの帰り道を作る~
gotalab555
9
1.8k
Continuous Delivery! It is not what you think it is
tdpauw
0
180
作り直せるコードは迅速に 作り直せないDBは慎重に - AI時代のプロダクトエンジニアが「判断の不可逆性」で開発速度を変える話
kinosuke01
0
130
プロダクト思考 × 基盤思考を AIで実現する Compound Engineering
tkc66buzz
1
210
KPIだけでは評価できないプロダクトが考えるべき Evalsという第二の評価系 / Beyond KPIs: Evals as a Second Evaluation Framework for Products #PdEConf
aki_iinuma
3
3.1k
Bet AI Day 2026丨AIによって本質に戻るシステムリスク管理
layerx
PRO
0
700
Featured
See All Featured
Faster Mobile Websites
deanohume
310
32k
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
230
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
400
Evolving SEO for Evolving Search Engines
ryanjones
0
280
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.7k
The Spectacular Lies of Maps
axbom
PRO
1
970
Technical Leadership for Architectural Decision Making
baasie
3
550
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
410
Ruling the World: When Life Gets Gamed
codingconduct
0
320
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
590
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
28
3.6k
So, you think you're a good person
axbom
PRO
2
2.1k
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!!