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

feat(nuxt): prefetch middleware/layouts + await layout loading #10155

Merged
merged 4 commits into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions packages/nuxt/src/app/plugins/prefetch.client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { hasProtocol } from 'ufo'
import { defineNuxtPlugin, useNuxtApp, useRouter } from '#app'
// @ts-ignore
import layouts from '#build/layouts'
// @ts-ignore
import { namedMiddleware } from '#build/middleware'

export default defineNuxtPlugin(() => {
const nuxtApp = useNuxtApp()
const router = useRouter()

// Force layout prefetch on route changes
nuxtApp.hooks.hook('app:mounted', () => {
router.beforeEach(async (to) => {
const layout = to?.meta?.layout
if (layout && typeof layouts[layout] === 'function') {
await layouts[layout]()
}
})
})
// Prefetch layouts & middleware
nuxtApp.hooks.hook('link:prefetch', (url) => {
if (hasProtocol(url)) { return }
const route = router.resolve(url)
if (!route) { return }
const layout = route?.meta?.layout
let middleware = Array.isArray(route?.meta?.middleware) ? route?.meta?.middleware : [route?.meta?.middleware]
middleware = middleware.filter(m => typeof m === 'string')

for (const name of middleware) {
if (typeof namedMiddleware[name] === 'function') {
namedMiddleware[name]()
}
}

if (layout && typeof layouts[layout] === 'function') {
layouts[layout]()
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the moment, this is only imported in <NuxtLayout> which means layout files are not bundled unless a layout is actually used. I wonder if we can move this elsewhere (maybe first use of <NuxtLayout> triggers hook registration?)

If not, I think it's a reasonable trade-off given the very nice improvement in this PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean importing the build/layouts will make the creation of the chunks?

i think it’s fine if they are always created IMO

we could also check if we have layouts defined but NuxtLayout is never used (I experienced this, layouts but forgot to use the NuxtLayout component in app.vue and got no warnings)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a nice idea! Only scenario I can think of where it would be a pain is when you have a theme that provides layouts but the end project doesn't want to use any of them. Or if <NuxtLayout> is only being used within a page and only on some of the site. (But still I think the warning would be fine.)

(I don't think it has to be in this PR though.)

})
})
3 changes: 3 additions & 0 deletions packages/nuxt/src/core/nuxt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ async function initNuxt (nuxt: Nuxt) {
addPlugin(resolve(nuxt.options.appDir, 'plugins/debug'))
}

// Add prefetching support for middleware & layouts
addPlugin(resolve(nuxt.options.appDir, 'plugins/prefetch.client'))
danielroe marked this conversation as resolved.
Show resolved Hide resolved

for (const m of modulesToInstall) {
if (Array.isArray(m)) {
await installModule(m[0], m[1])
Expand Down