fix(nuxt): handle undefined paths in resolveTrailingSlashBehavior

This commit is contained in:
Daniel Roe 2024-02-22 12:03:57 +00:00
parent 3fc4231d33
commit ba6a4132bb
No known key found for this signature in database
GPG Key ID: 3714AB03996F442B
2 changed files with 17 additions and 9 deletions

View File

@ -88,7 +88,7 @@ export interface NuxtLinkProps extends Omit<RouterLinkProps, 'to'> {
noPrefetch?: boolean noPrefetch?: boolean
} }
/*@__NO_SIDE_EFFECTS__*/ /*@__NO_SIDE_EFFECTS__*/
export function defineNuxtLink (options: NuxtLinkOptions) { export function defineNuxtLink (options: NuxtLinkOptions) {
const componentName = options.componentName || 'NuxtLink' 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: string, resolve: Router['resolve']): string
function resolveTrailingSlashBehavior (to: RouteLocationRaw, resolve: Router['resolve']): Exclude<RouteLocationRaw, string> function resolveTrailingSlashBehavior (to: RouteLocationRaw, resolve: Router['resolve']): Exclude<RouteLocationRaw, string>
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')) { if (!to || (options.trailingSlash !== 'append' && options.trailingSlash !== 'remove')) {
return to return to
} }
@ -109,13 +110,18 @@ export function defineNuxtLink (options: NuxtLinkOptions) {
return applyTrailingSlashBehavior(to, options.trailingSlash) 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, ...to,
name: undefined, // named routes would otherwise always override trailing slash behavior
path: applyTrailingSlashBehavior(path, options.trailingSlash) path: applyTrailingSlashBehavior(path, options.trailingSlash)
} }
if ('name' in resolvedPath) {
delete resolvedPath.name
}
return resolvedPath
} }
return defineComponent({ return defineComponent({
@ -326,8 +332,8 @@ export function defineNuxtLink (options: NuxtLinkOptions) {
const href = typeof to.value === 'object' const href = typeof to.value === 'object'
? router.resolve(to.value)?.href ?? null ? router.resolve(to.value)?.href ?? null
: (to.value && !props.external && !isAbsoluteUrl.value) : (to.value && !props.external && !isAbsoluteUrl.value)
? resolveTrailingSlashBehavior(joinURL(config.app.baseURL, to.value), router.resolve) as string ? resolveTrailingSlashBehavior(joinURL(config.app.baseURL, to.value), router.resolve) as string
: to.value || null : to.value || null
// Resolves `target` value // Resolves `target` value
const target = props.target || null const target = props.target || null

View File

@ -237,9 +237,11 @@ const plugin: Plugin<{ router: Router }> = defineNuxtPlugin({
nuxtApp.hooks.hookOnce('app:created', async () => { nuxtApp.hooks.hookOnce('app:created', async () => {
try { try {
const to = router.resolve(initialURL)
// #4920, #4982
if ('name' in to) { delete to.name }
await router.replace({ await router.replace({
...router.resolve(initialURL), ...to,
name: undefined, // #4920, #4982
force: true force: true
}) })
// reset scroll behavior to initial value // reset scroll behavior to initial value