From 4ca1ab7cfb4cd7157a156382fcde0250f2fb4d79 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Thu, 22 Feb 2024 12:46:13 +0000 Subject: [PATCH] fix(nuxt): set `to.name` to be undefined rather than deleting entirely --- packages/nuxt/src/app/components/nuxt-link.ts | 8 ++++---- packages/nuxt/src/pages/runtime/plugins/router.ts | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/nuxt/src/app/components/nuxt-link.ts b/packages/nuxt/src/app/components/nuxt-link.ts index eed79ef4cd..e3ecfd29a9 100644 --- a/packages/nuxt/src/app/components/nuxt-link.ts +++ b/packages/nuxt/src/app/components/nuxt-link.ts @@ -88,7 +88,7 @@ export interface NuxtLinkProps extends Omit { noPrefetch?: boolean } -/*@__NO_SIDE_EFFECTS__*/ + /*@__NO_SIDE_EFFECTS__*/ export function defineNuxtLink (options: NuxtLinkOptions) { const componentName = options.componentName || 'NuxtLink' @@ -100,7 +100,6 @@ export function defineNuxtLink (options: NuxtLinkOptions) { function resolveTrailingSlashBehavior (to: string, resolve: Router['resolve']): string function resolveTrailingSlashBehavior (to: RouteLocationRaw, resolve: Router['resolve']): Exclude - function resolveTrailingSlashBehavior (to: undefined, resolve: Router['resolve']): undefined function resolveTrailingSlashBehavior (to: RouteLocationRaw | undefined, resolve: Router['resolve']): RouteLocationRaw | RouteLocation | undefined { if (!to || (options.trailingSlash !== 'append' && options.trailingSlash !== 'remove')) { return to @@ -117,6 +116,7 @@ export function defineNuxtLink (options: NuxtLinkOptions) { path: applyTrailingSlashBehavior(path, options.trailingSlash) } + // named routes would otherwise always override trailing slash behavior if ('name' in resolvedPath) { delete resolvedPath.name } @@ -332,8 +332,8 @@ export function defineNuxtLink (options: NuxtLinkOptions) { const href = typeof to.value === 'object' ? router.resolve(to.value)?.href ?? null : (to.value && !props.external && !isAbsoluteUrl.value) - ? resolveTrailingSlashBehavior(joinURL(config.app.baseURL, to.value), router.resolve) as string - : to.value || null + ? resolveTrailingSlashBehavior(joinURL(config.app.baseURL, to.value), router.resolve) as string + : to.value || null // Resolves `target` value const target = props.target || null diff --git a/packages/nuxt/src/pages/runtime/plugins/router.ts b/packages/nuxt/src/pages/runtime/plugins/router.ts index 53a62bdcf8..267ea380d7 100644 --- a/packages/nuxt/src/pages/runtime/plugins/router.ts +++ b/packages/nuxt/src/pages/runtime/plugins/router.ts @@ -239,7 +239,7 @@ const plugin: Plugin<{ router: Router }> = defineNuxtPlugin({ try { const to = router.resolve(initialURL) // #4920, #4982 - if ('name' in to) { delete to.name } + if ('name' in to) { to.name = undefined } await router.replace({ ...to, force: true