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
840
Marpをカスタマイズして爆速スライド開発環境を手に入れよう
gityosan
0
720
TiptapでストレスフリーなWYSIWYGエディター開発を!
gityosan
0
500
Other Decks in Technology
See All in Technology
Deploying a Full-Stack Bun-Native Framework on Cloudflare Workers
7nohe
0
120
SQL文一行も書けない人事がCortexもろもろを使って人事業務を楽にしてみる
ponponmikankan
1
200
AI時代だからこそ、スケールしないことをやろう
yutashigemura
1
120
アリアドネの糸と、20年ごとの建て替え ── 長尾真『電子図書館』を、伊勢で読み直す / Rereading Makoto Nagao’s "Electronic Library" in Ise
ykiyota
0
120
Amazon Quick on DesktopがIAM Identity Centerで動かない理由
yukiogawa
0
140
Screen Lens - 今見てる画面を翻訳する
komagata
0
190
HRC_Frontend_Conference_Fukuoka_2026.pdf
ts020
0
160
Reactの設計論
uhyo
13
6k
Oracle Cloud Infrastructure IaaS 新機能アップデート 2026/6 - 2026/8
oracle4engineer
PRO
0
130
Redmine 7.0で私が開発した新機能の狙いと背景
vividtone
1
130
2026-09-11 【Snowflake World Tour Tokyo 2026】Snowflakeを起点に、AI Agentが自律稼働し続ける未来へ / Driving AI Agents with Snowflake
civitaspo
0
250
AIとペアプロを始める。人とのペアプロをやめる。ペアプロの良さを改めて知る。もっと好きになった。 / Rediscovering Pair Programming
honyanya
1
320
Featured
See All Featured
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.5k
Principles of Awesome APIs and How to Build Them.
keavy
128
18k
New Earth Scene 8
popppiees
3
2.5k
Raft: Consensus for Rubyists
vanstee
141
7.7k
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
3k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
23k
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.4k
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
510
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
120
120k
Java REST API Framework Comparison - PWX 2021
mraible
34
9.7k
Between Models and Reality
mayunak
4
450
GitHub's CSS Performance
jonrohan
1033
470k
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!!