From ba6a4132bb0847fdf063e3b3f90b7078f81e91b4 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Thu, 22 Feb 2024 12:03:57 +0000 Subject: [PATCH] fix(nuxt): handle `undefined` paths in `resolveTrailingSlashBehavior` --- packages/nuxt/src/app/components/nuxt-link.ts | 20 ++++++++++++------- .../nuxt/src/pages/runtime/plugins/router.ts | 6 ++++-- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/packages/nuxt/src/app/components/nuxt-link.ts b/packages/nuxt/src/app/components/nuxt-link.ts index c0d8d42417..eed79ef4cd 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,8 @@ export function defineNuxtLink (options: NuxtLinkOptions) { function resolveTrailingSlashBehavior (to: string, resolve: Router['resolve']): string function resolveTrailingSlashBehavior (to: RouteLocationRaw, resolve: Router['resolve']): Exclude - function resolveTrailingSlashBehavior (to: RouteLocationRaw, resolve: Router['resolve']): RouteLocationRaw | RouteLocation { + 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 } @@ -109,13 +110,18 @@ export function defineNuxtLink (options: NuxtLinkOptions) { return applyTrailingSlashBehavior(to, options.trailingSlash) } - const path = 'path' in to ? to.path : resolve(to).path + const path = 'path' in to && to.path !== undefined ? to.path : resolve(to).path - return { + const resolvedPath = { ...to, - name: undefined, // named routes would otherwise always override trailing slash behavior path: applyTrailingSlashBehavior(path, options.trailingSlash) } + + if ('name' in resolvedPath) { + delete resolvedPath.name + } + + return resolvedPath } return defineComponent({ @@ -326,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 0de9129194..53a62bdcf8 100644 --- a/packages/nuxt/src/pages/runtime/plugins/router.ts +++ b/packages/nuxt/src/pages/runtime/plugins/router.ts @@ -237,9 +237,11 @@ const plugin: Plugin<{ router: Router }> = defineNuxtPlugin({ nuxtApp.hooks.hookOnce('app:created', async () => { try { + const to = router.resolve(initialURL) + // #4920, #4982 + if ('name' in to) { delete to.name } await router.replace({ - ...router.resolve(initialURL), - name: undefined, // #4920, #4982 + ...to, force: true }) // reset scroll behavior to initial value