From 0d854d9a0675bbb2d0c613b38e694794d9339973 Mon Sep 17 00:00:00 2001 From: Dominic <67210734+rexhent@users.noreply.github.com> Date: Wed, 26 Jun 2024 19:51:42 +1000 Subject: [PATCH] fix(nuxt): handle external links to named route objects (#27829) --- packages/nuxt/src/app/components/nuxt-link.ts | 6 +++-- packages/nuxt/test/nuxt-link.test.ts | 22 +++++++++++-------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/packages/nuxt/src/app/components/nuxt-link.ts b/packages/nuxt/src/app/components/nuxt-link.ts index cf27ce9a34..0b5d1c6511 100644 --- a/packages/nuxt/src/app/components/nuxt-link.ts +++ b/packages/nuxt/src/app/components/nuxt-link.ts @@ -167,8 +167,10 @@ export function defineNuxtLink (options: NuxtLinkOptions) { if (!to.value || isAbsoluteUrl.value) { return to.value as string } if (isExternal.value) { - const path = typeof to.value === 'object' ? resolveRouteObject(to.value) : to.value - return resolveTrailingSlashBehavior(path, router.resolve /* will not be called */) as string + const path = typeof to.value === 'object' && 'path' in to.value ? resolveRouteObject(to.value) : to.value + // separately resolve route objects with a 'name' property and without 'path' + const href = typeof path === 'object' ? router.resolve(path).href : path + return resolveTrailingSlashBehavior(href, router.resolve /* will not be called */) as string } if (typeof to.value === 'object') { diff --git a/packages/nuxt/test/nuxt-link.test.ts b/packages/nuxt/test/nuxt-link.test.ts index 40e714045d..f57c5d43b0 100644 --- a/packages/nuxt/test/nuxt-link.test.ts +++ b/packages/nuxt/test/nuxt-link.test.ts @@ -30,17 +30,16 @@ vi.mock('../src/app/composables/router', () => ({ return withQuery(to.path || '', to.query || {}) + (to.hash || '') }, useRouter: () => ({ - resolve: (route: string | RouteLocation & { to?: string }): Partial & { href?: string } => { + resolve: (route: string | RouteLocation): Partial & { href: string } => { if (typeof route === 'string') { - return { href: route, path: route } + return { path: route, href: route } + } + return { + path: route.path || `/${route.name?.toString()}`, + query: route.query || undefined, + hash: route.hash || undefined, + href: route.path || `/${route.name?.toString()}`, } - return route.to - ? { href: route.to } - : { - path: route.path || `/${route.name?.toString()}` || undefined, - query: route.query || undefined, - hash: route.hash || undefined, - } }, }), })) @@ -133,6 +132,10 @@ describe('nuxt-link:propsOrAttributes', () => { expect(nuxtLink({ to: { path: '/to' }, external: true }).props.href).toBe('/to') }) + it('resolves route location object with name', () => { + expect(nuxtLink({ to: { name: 'to' }, external: true }).props.href).toBe('/to') + }) + it('applies trailing slash behaviour', () => { expect(nuxtLink({ to: { path: '/to' }, external: true }, { trailingSlash: 'append' }).props.href).toBe('/to/') expect(nuxtLink({ to: '/to', external: true }, { trailingSlash: 'append' }).props.href).toBe('/to/') @@ -225,6 +228,7 @@ describe('nuxt-link:propsOrAttributes', () => { it('forwards `to` prop', () => { expect(nuxtLink({ to: '/to' }).props.to).toBe('/to') expect(nuxtLink({ to: { path: '/to' } }).props.to).toEqual({ path: '/to' }) + expect(nuxtLink({ to: { name: 'to' } }).props.to).toEqual({ name: 'to' }) }) })