From a5201854429f2487a0733e4553c6b9f628f54e26 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Tue, 30 Aug 2022 15:41:11 +0100 Subject: [PATCH] perf(nuxt): don't prefetch all global components (#7069) --- packages/nuxt/src/components/module.ts | 12 ++++++++++++ test/basic.test.ts | 7 +++++++ 2 files changed, 19 insertions(+) diff --git a/packages/nuxt/src/components/module.ts b/packages/nuxt/src/components/module.ts index 5656b4dae4..4f71dc19b2 100644 --- a/packages/nuxt/src/components/module.ts +++ b/packages/nuxt/src/components/module.ts @@ -130,6 +130,18 @@ export default defineNuxtModule({ } }) + // 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 nuxt.hook('app:templates', async () => { const newComponents = await scanComponents(componentDirs, nuxt.options.srcDir!) diff --git a/test/basic.test.ts b/test/basic.test.ts index f41310a14a..893f40ef7c 100644 --- a/test/basic.test.ts +++ b/test/basic.test.ts @@ -380,6 +380,13 @@ describe('prefetching', () => { it('should prefetch components', async () => { 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(/]*\/_nuxt\/TestGlobal[^>]*\.js"/) + // should not prefetch all other pages + expect(html).not.toMatch(/]*\/_nuxt\/navigate-to[^>]*\.js"/) + }) }) if (process.env.NUXT_TEST_DEV) {