perf(nuxt): don't prefetch all global components (#7069)

This commit is contained in:
Daniel Roe 2022-08-30 15:41:11 +01:00 committed by GitHub
parent fd6d1d4fbe
commit a520185442
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 0 deletions

View File

@ -130,6 +130,18 @@ export default defineNuxtModule<ComponentsOptions>({
} }
}) })
// Do not prefetch global components chunks
nuxt.hook('build:manifest', (manifest) => {
const sourceFiles = getComponents().filter(c => c.global).map(c => relative(nuxt.options.srcDir, c.filePath))
for (const key in manifest) {
if (manifest[key].isEntry) {
manifest[key].dynamicImports =
manifest[key].dynamicImports?.filter(i => !sourceFiles.includes(i))
}
}
})
// Scan components and add to plugin // Scan components and add to plugin
nuxt.hook('app:templates', async () => { nuxt.hook('app:templates', async () => {
const newComponents = await scanComponents(componentDirs, nuxt.options.srcDir!) const newComponents = await scanComponents(componentDirs, nuxt.options.srcDir!)

View File

@ -380,6 +380,13 @@ describe('prefetching', () => {
it('should prefetch components', async () => { it('should prefetch components', async () => {
await expectNoClientErrors('/prefetch/components') await expectNoClientErrors('/prefetch/components')
}) })
it('should not prefetch certain dynamic imports by default', async () => {
const html = await $fetch('/auth')
// should not prefetch global components
expect(html).not.toMatch(/<link [^>]*\/_nuxt\/TestGlobal[^>]*\.js"/)
// should not prefetch all other pages
expect(html).not.toMatch(/<link [^>]*\/_nuxt\/navigate-to[^>]*\.js"/)
})
}) })
if (process.env.NUXT_TEST_DEV) { if (process.env.NUXT_TEST_DEV) {