Skip to content

Commit

Permalink
fix: NuxtLinkLocale ignoring href prop (#2751)
Browse files Browse the repository at this point in the history
  • Loading branch information
BobbieGoede authored Feb 2, 2024
1 parent 0674ecd commit 5db7351
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/runtime/components/NuxtLinkLocale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { NuxtLinkProps } from 'nuxt/app'

const NuxtLinkLocale = defineNuxtLink({ componentName: 'NuxtLinkLocale' })

export default defineComponent<NuxtLinkProps & { locale?: string; to: NuxtLinkProps['to'] }>({
export default defineComponent<NuxtLinkProps & { locale?: string }>({
name: 'NuxtLinkLocale',
props: {
...NuxtLinkLocale.props,
Expand All @@ -20,7 +20,18 @@ export default defineComponent<NuxtLinkProps & { locale?: string; to: NuxtLinkPr
},
setup(props, { slots }) {
const localePath = useLocalePath()
const resolvedPath = computed(() => (props.to != null ? localePath(props.to, props.locale) : props.to))

// From https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/app/components/nuxt-link.ts#L57
const checkPropConflicts = (props: NuxtLinkProps, main: keyof NuxtLinkProps, sub: keyof NuxtLinkProps): void => {
if (import.meta.dev && props[main] !== undefined && props[sub] !== undefined) {
console.warn(`[NuxtLinkLocale] \`${main}\` and \`${sub}\` cannot be used together. \`${sub}\` will be ignored.`)
}
}

const resolvedPath = computed(() => {
const destination = props.to ?? props.href
return destination != null ? localePath(destination, props.locale) : destination
})

// Resolving link type
const isExternal = computed<boolean>(() => {
Expand All @@ -34,12 +45,13 @@ export default defineComponent<NuxtLinkProps & { locale?: string; to: NuxtLinkPr
return true
}

const destination = props.to ?? props.href
// When `to` is a route object then it's an internal link
if (typeof props.to === 'object') {
if (typeof destination === 'object') {
return false
}

return props.to === '' || props.to == null || hasProtocol(props.to, { acceptRelative: true })
return destination === '' || destination == null || hasProtocol(destination, { acceptRelative: true })
})

/**
Expand All @@ -55,6 +67,10 @@ export default defineComponent<NuxtLinkProps & { locale?: string; to: NuxtLinkPr
_props.to = resolvedPath.value
}

// Warn when both properties are used, delete `href` to prevent warning by `NuxtLink`
checkPropConflicts(props, 'to', 'href')
delete _props.href

// The locale attribute cannot be set for NuxtLink
// @see https://github.com/nuxt-modules/i18n/issues/2498
delete _props.locale
Expand Down

0 comments on commit 5db7351

Please sign in to comment.