Skip to content
This repository was archived by the owner on Apr 6, 2023. It is now read-only.

Commit 912eafb

Browse files
authored
feat(nuxt): prefetch middleware/layouts + await layout loading (#10155)
1 parent 49a2dd5 commit 912eafb

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

packages/nuxt/src/pages/module.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,11 @@ export default defineNuxtModule({
140140
addVitePlugin(PageMetaPlugin.vite(pageMetaOptions))
141141
addWebpackPlugin(PageMetaPlugin.webpack(pageMetaOptions))
142142

143+
// Add prefetching support for middleware & layouts
144+
addPlugin(resolve(runtimeDir, 'plugins/prefetch.client'))
145+
143146
// Add router plugin
144-
addPlugin(resolve(runtimeDir, 'router'))
147+
addPlugin(resolve(runtimeDir, 'plugins/router'))
145148

146149
const getSources = (pages: NuxtPage[]): string[] => pages.flatMap(p =>
147150
[relative(nuxt.options.srcDir, p.file), ...getSources(p.children || [])]
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { hasProtocol } from 'ufo'
2+
import { defineNuxtPlugin, useNuxtApp, useRouter } from '#app'
3+
// @ts-ignore
4+
import layouts from '#build/layouts'
5+
// @ts-ignore
6+
import { namedMiddleware } from '#build/middleware'
7+
8+
export default defineNuxtPlugin(() => {
9+
const nuxtApp = useNuxtApp()
10+
const router = useRouter()
11+
12+
// Force layout prefetch on route changes
13+
nuxtApp.hooks.hook('app:mounted', () => {
14+
router.beforeEach(async (to) => {
15+
const layout = to?.meta?.layout
16+
if (layout && typeof layouts[layout] === 'function') {
17+
await layouts[layout]()
18+
}
19+
})
20+
})
21+
// Prefetch layouts & middleware
22+
nuxtApp.hooks.hook('link:prefetch', (url) => {
23+
if (hasProtocol(url)) { return }
24+
const route = router.resolve(url)
25+
if (!route) { return }
26+
const layout = route?.meta?.layout
27+
let middleware = Array.isArray(route?.meta?.middleware) ? route?.meta?.middleware : [route?.meta?.middleware]
28+
middleware = middleware.filter(m => typeof m === 'string')
29+
30+
for (const name of middleware) {
31+
if (typeof namedMiddleware[name] === 'function') {
32+
namedMiddleware[name]()
33+
}
34+
}
35+
36+
if (layout && typeof layouts[layout] === 'function') {
37+
layouts[layout]()
38+
}
39+
})
40+
})

packages/nuxt/src/pages/runtime/router.ts renamed to packages/nuxt/src/pages/runtime/plugins/router.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
} from 'vue-router'
1212
import { createError } from 'h3'
1313
import { withoutBase, isEqual } from 'ufo'
14-
import type NuxtPage from './page'
14+
import type NuxtPage from '../page'
1515
import { callWithNuxt, defineNuxtPlugin, useRuntimeConfig, showError, clearError, navigateTo, useError, useState } from '#app'
1616
// @ts-ignore
1717
import _routes from '#build/routes'

0 commit comments

Comments
 (0)