MCPcopy
hub / github.com/nuxt/nuxt / setup

Function setup

packages/nuxt/src/pages/runtime/plugins/prefetch.client.ts:13–44  ·  view source on GitHub ↗
(nuxtApp)

Source from the content-addressed store, hash-verified

11export default defineNuxtPlugin({
12 name: 'nuxt:prefetch',
13 setup (nuxtApp) {
14 const router = useRouter()
15
16 // Force layout prefetch on route changes
17 nuxtApp.hooks.hook('app:mounted', () => {
18 router.beforeEach(async (to) => {
19 const layout = to?.meta?.layout
20 if (layout && typeof layouts[layout] === 'function') {
21 await layouts[layout]()
22 }
23 })
24 })
25 // Prefetch layouts & middleware
26 nuxtApp.hooks.hook('link:prefetch', (url) => {
27 if (hasProtocol(url)) { return }
28 const route = router.resolve(url)
29 if (!route) { return }
30 const layout = route.meta.layout
31 let middleware = toArray(route.meta.middleware)
32 middleware = middleware.filter(m => typeof m === 'string')
33
34 for (const name of middleware) {
35 if (typeof namedMiddleware[name] === 'function') {
36 namedMiddleware[name]()
37 }
38 }
39
40 if (typeof layout === 'string' && layout in layouts) {
41 _loadAsyncComponent(layouts[layout])
42 }
43 })
44 },
45})

Callers

nothing calls this directly

Calls 4

useRouterFunction · 0.90
toArrayFunction · 0.90
_loadAsyncComponentFunction · 0.90
resolveMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…