From 0be8cda77a951d126129742f786389c58e2d2a14 Mon Sep 17 00:00:00 2001 From: Lucie <25330882+lihbr@users.noreply.github.com> Date: Thu, 2 Mar 2023 10:53:46 +0100 Subject: [PATCH] fix(nuxt): prevent fallthrough attributes on custom `NuxtLink` (#19379) --- packages/nuxt/src/app/components/nuxt-link.ts | 31 ++++++++++++------- test/bundle.test.ts | 2 +- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/packages/nuxt/src/app/components/nuxt-link.ts b/packages/nuxt/src/app/components/nuxt-link.ts index 14d25a1cdd..60e6e069c1 100644 --- a/packages/nuxt/src/app/components/nuxt-link.ts +++ b/packages/nuxt/src/app/components/nuxt-link.ts @@ -211,20 +211,29 @@ export function defineNuxtLink (options: NuxtLinkOptions) { return () => { if (!isExternal.value) { + const routerLinkProps: Record = { + ref: process.server ? undefined : (ref: any) => { el!.value = ref?.$el }, + to: to.value, + activeClass: props.activeClass || options.activeClass, + exactActiveClass: props.exactActiveClass || options.exactActiveClass, + replace: props.replace, + ariaCurrentValue: props.ariaCurrentValue, + custom: props.custom + } + + // `custom` API cannot support fallthrough attributes as the slot + // may render fragment or text root nodes (#14897, #19375) + if (!props.custom) { + if (prefetched.value) { + routerLinkProps.class = props.prefetchedClass || options.prefetchedClass + } + routerLinkProps.rel = props.rel + } + // Internal link return h( resolveComponent('RouterLink'), - { - ref: process.server ? undefined : (ref: any) => { el!.value = ref?.$el }, - to: to.value, - ...((prefetched.value && !props.custom) ? { class: props.prefetchedClass || options.prefetchedClass } : {}), - activeClass: props.activeClass || options.activeClass, - exactActiveClass: props.exactActiveClass || options.exactActiveClass, - replace: props.replace, - ariaCurrentValue: props.ariaCurrentValue, - custom: props.custom, - rel: props.rel - }, + routerLinkProps, slots.default ) } diff --git a/test/bundle.test.ts b/test/bundle.test.ts index d771e25342..479b982955 100644 --- a/test/bundle.test.ts +++ b/test/bundle.test.ts @@ -40,7 +40,7 @@ describe.skipIf(isWindows)('minimal nuxt application', () => { it('default server bundle size', async () => { stats.server = await analyzeSizes(['**/*.mjs', '!node_modules'], serverDir) - expect(stats.server.totalBytes).toBeLessThan(92900) + expect(stats.server.totalBytes).toBeLessThan(93000) const modules = await analyzeSizes('node_modules/**/*', serverDir) expect(modules.totalBytes).toBeLessThan(2722000)