This repository has been archived by the owner on Apr 6, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1k
feat(nuxt): prefetch middleware/layouts + await layout loading #10155
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
62051e1
feat(app): improve client-side performance by prefetching middleware β¦
atinux c6dac34
lint fix
atinux 3624ae4
fix: inject prefetch plugin only with pages module
danielroe 153f004
Merge remote-tracking branch 'origin/main' into feat/perfs
danielroe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]() | ||
} | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.)